Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling both specific and general Python exceptions?

Tags:

python

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)·

like image 997
OldTinfoil Avatar asked Jul 25 '13 14:07

OldTinfoil


2 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
like image 63
Martijn Pieters Avatar answered Oct 20 '22 17:10

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 try...except block causes the caught exception to be passed up to the outer block.

like image 44
larsks Avatar answered Oct 20 '22 17:10

larsks