Handling both specific and general Python exceptions?
python
Solution
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
Problem
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)·