Try-except block: analog for 'else' if an exception was raised

I have a code like this:

try:
    return make_success_result()
except FirstException:
    handle_first_exception()
    return make_error_result()
except SecondException:
    handle_second_exception()
    return make_error_result()

And I wonder if I can achieve this:

try:
    # do something
except Error1:
    # do Error1 specific handling
except Error2:
    # do Error2 specific handling
else:
    # do this if there was no exception
????:
    # ALSO do this if there was ANY of the listed exceptions (e.g. some common error handling)

Thus, the code is executed in one of the following sequences:

try > else > finally
try > except > ???? > finally

EDIT: My point is that a block ????should execute immediately after ANY of the blocks except, which means it's an addition to error handling, not a substitution.

+4
source share
5 answers

What I would do in this case is to set a boolean when you get an exception, for example:

got_exception = False
try:
    # do something
except Error1:
    # do Error1 specific handling
    got_exception = True
except Error2:
    # do Error2 specific handling
    got_exception = True
else:
    # If there was no exception
finally:
    if got_exception:
        # ALSO do this if there was ANY exception (e.g. some common error handling)

, IMO , , .

+5

:

try:
    # Do something
except Error1:
    # Do Error1 specific handling
except Error2:
    # Do Error2 specific handling
except Exception:
    # Handle any other exception (Generic exception handling)
else:
    # Do this if there were no exceptions
finally:
    # Execute this regardless
+2

:

try:
    print 'try'
    # 1/0
    # {}[1]
    # {}.a
except AttributeError, KeyError:  # only handle these exceptions..
    try:
        raise                     # re-raise the exception so we can add a finally-clause executing iff there was an exception.
    except AttributeError:
        print 'attrerr'
        # raise ...               # any raises here will also execute 'common'
    except KeyError:
        print 'keyerror'
    finally:                      # update 0: you wanted the common code after the exceptions..
        print "common"

else:
    print 'no exception'

, , , , ..

: , try-. .

UPDATE2: , common . . @MattTaylor - , , ; -)

+1

, python else, finally. :

try:
    # do something
except Error1:
    # do Error1 specific handling
except Error2:
    # do Error2 specific handling
else:
    # do this if there was no exception
finally:
    # Do this in any case!

python docs , , .


EDIT: , . Python :

try , , .

, finally , . else , .

0

:

try:
    # do something
except Exception as e:
    if isinstance(e, Error1):
        # do Error1 specific handling
    elif isinstance(e, Error2):
        # do Error2 specific handling
    else:
        # do non-Error1/Error2 handling
    # ALSO do this if there was ANY exception (e.g. some common error handling)
else:
    # do this if there was no exception
0

Source: https://habr.com/ru/post/1615373/


All Articles