Skip to content

Python reraise

  • by

Python reraise exception means raising the same exception again. You can use the raise statement without specifying the exception object.

Catching all exception handlers

try:
 ...
except Exception as e:
    # Process exception information in some way
    ...
    # Propagate the exception
    raise

Python reraise

Simple example code Reraising the exception, that has been caught in the except block.

def example():
    try:
        int('N/A')
    except ValueError:
        print("Didn't work")
    raise


example()

Output:

Python reraise

How to re-raise an exception in nested try/except blocks?

Answer: Simple raise e will do exceptions in nested try/except blocks.

try:
    something()
except SomeError as e:
    try:
        plan_B()
    except AlsoFailsError:
        raise e  # or raise e from None - see below

Do comment if you have any doubts or suggestions on this Python keyword topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading