Skip to content Skip to sidebar Skip to footer

Multiple Exception Handlers For The Same Exception

I have a code for a function which is called inside another function.(Result of refactoring). So in the called function I have a huge block of try-catch statements as. def Called()

Solution 1:

This won't work as advertised; only the first except A clause will ever get executed. What you need is either some logic inside the clause to further inspect the exception, or (if the code inside the try block permits) several try-except blocks.

Example of the former approach:

try:
    something_that_might_fail()
except A as e:
    if e.is_harmless():
        passelif e.is_something_we_can_handle():
        handle_it()
    else:
        raise# re-raise in the hope it gets handled further up the stack

Solution 2:

I think this will work

defCalled():
     try:
         #All statements for the function in the try block.except A:
         try: 
             do_someting()
         except B:
             try:
                do_somthing_else()
             except:

     except A: 
         # Exception handler.

Post a Comment for "Multiple Exception Handlers For The Same Exception"