简体   繁体   中英

Catch an already caught exception

I have a function that should both run independently and be used by other functions that handles some exceptions

def some_function():
    try:
        do_something()
    except SpecificException:
        handle_exception()

And I have a second function that calls the first function but wants to differently handle this exception when occurring in some_function :

def different_function():
    try:
        some_function()
    except SpecificException:
        different_handling()

Is this at all possible? What I am looking for is a way to disable the except in some_function so that the try-except in different_function can catch SpecificException .

Tell the function some_function to not handle the exception and pass it along

def some_function(handleException=True):
    try:
        do_something()
    except SpecificException:
        if !handleException: raise
        handle_exception()

def different_function():
    try:
        some_function(handleException=False)
    except SpecificException:
        different_handling()

Or give the function some_function the exception handler to use

def some_function(exceptionHandler=handle_exception):
    try:
        do_something()
    except SpecificException:
        exceptionHandler()

def different_function():
    some_function(exceptionHandler=different_handling)

You can re-raise the exception again in the first function after you execute the desired logic there.

def some_function():
    try:
        do_something()
    except SpecificException as e:
        handle_exception()
        raise e 

You can re-raise it with the raise statement.

def some_function():
    try:
        do_something()
    except SpecificException:
        handle_exception()
        raise

As the doc says: If no expressions are present, raise re-raises the last exception that was active in the current scope.

I can't get what you are trying to say exactly but if you handle the specific exception inside the first function the except that you wrote at the second function will not work. If these two exceptions are different the each other like FileNotFoundException and IOException that will change the situation. In that case, whichever error the code gives, the part related to that will work. If the problem was different than this can you open the problem a little bit?

It all depends on how you handle the first function. if your first function accepts the error and handles it, then the second function will not find any error, so it will execute.

But if your first function can not handle the error,(say your first function handles error_type1, but during the runtime it raised error_type2), then the first function's exception will obviously fail, and if that error is handled properly in the second function , then it's all ok.

All I meant, if the first function somehow raises an error, then the second function will catch it, otherwise not, it is that simple. now if you want the same error caught in the second function, just raise the error inside first exception.

Your example is legal, but let's split it to several common cases, to check along the examples I'll provide:

def some_function():
    try:
        a = int("a")
    except ValueError:
        print("not convertable")        

def different_function():
    try:
        some_function()
    except Exception as e:
        print(e)

different_function()

output:

not convertable

what happened?

function different_function invoked some_function inside a try catch statement, the inner function now, run into a line that raise Exception inside a try catch statement of it's own, and handle it. now - the exception never re-raised to outer function that keeps run without facing an Error.

this is an example' when the inner function knows how to handle the exception.

sometimes, in real life cases, the inner function do not know how to handle the exception by itself and then re-raise the exception up-wards like in here:

def some_function():
    try:
        a = int("a")
    except ValueError as e:
        raise e
        

def different_function():
    try:
        some_function()
    except Exception as e:
        print("I Know how to fix it: " + str(e))

different_function()

output:

I Know how to fix it: invalid literal for int() with base 10: 'a'

and with that order the error handling chain is keep tracked all over the program, and handled where ever it can be handled

Best practice is to keep statements inside try and except as little as possible, to not ran into exception also through them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM