Handling both specific and general Python exceptions?

Question:

I would like to catch a specific exception and handle it accordingly – then I would like to continue and perform the generic handling that other exceptions would have to.

Coming from a C background, I could have previously utilised gotos to achieve the desired effect.

This is what I’m currently doing is and it works fine:

try:
    output_var = some_magical_function()
except IntegrityError as ie:
    integrity_error_handling()
    shared_exception_handling_function(zde) # could be error reporting
except SomeOtherException as soe:
    shared_exception_handling_function(soe) # the same function as above

Tldr:

Ie – is there "Pythonic" way of doing the following:

try:
    output_var = some_magical_function()
except IntegrityError as ie:
    integrity_error_handling()
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
    shared_exception_handling_function(ae) # could be error reporting

NB: I am aware of the finally clause – this isn’t intended for tidy-up (ie- closing files)ยท

Asked By: OldTinfoil

||

Answers:

You could reraise the exception, and handle the generic case in the outer handler of a nested setup:

try:
    try:
        output_var = some_magical_function()
    except IntegrityError as zde:
        integrity_error_handling()
        raise
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
    shared_exception_handling_function(ae) # could be error reporting

The unqualified raise statement re-raises the current exception, so the IntegrityError exception is thrown again to be handled by the AllExceptions handler.

The other path you could take is to test for the exception type:

try:
    output_var = some_magical_function()
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
    if isinstance(ae, IntegrityError):
        integrity_error_handling()
    shared_exception_handling_function(ae) # could be error reporting
Answered By: Martijn Pieters

The Exception class will match all exceptions…

try:
    output_var = some_magical_function()
except IntegrityError as zde:
    integrity_error_handling()
except Exception as ae:
    shared_exception_handling_function(ae) # could be error reporting

But it sounds like you want the final clause to run for both IntegrityError exceptions as well as everything else. So you’ll need a different construct, possibly this:

try:
    try:
        output_var = some_magical_function()
    except IntegrityError as zde:
        integrity_error_handling()
        raise
except Exception as ae:
    shared_exception_handling_function(ae) # could be error reporting

The raise command on the inner tryexcept block causes the
caught exception to be passed up to the outer block.

Answered By: larsks
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.