11

I need to return True Value after raise statement. Here I need to raise statement as well as it should return True value. If I use finally statement, it will not raise exception block and if I do not use finally then exception block will execute with raise statement and after that I will not able to use retcodecmd variable. Below My Code in python:

try:
    something....
except ValueError:
    self._retcodecmd = True
    raise SomeException(something)
finally:
    if self._retcodecmd is True:
        return True
    else:
        return False
0

1 Answer 1

20

Returning and bubbling exceptions out of a function are mutually exclusive. It's nonsensical to exit a function by both raise and return, you have to choose.

The finally block here will force a return, undoing the exception you raised. If that's not what you want, you need to let the exception propagate without being overridden in the finally block and understand how to handle the exception appropriately in a caller.

Sign up to request clarification or add additional context in comments.

so any alternative for this situation where I need to raise error as well as I need to execute some other function after raising error.
@PranjayKaparuwan: Perhaps your return values are too restricted? You can't raise and return, but you could return multiple values, where the first is the same as what you're currently using, and the second indicates if an exception arose return True, sys.exc_info() == (None, None, None) or something similar but better suited to context. But fundamentally, exceptions and returns are mutually exclusive in every language I know of. If you're following the exception path, you can't follow the return path without cancelling the exception propagation, and vice versa.
@PranjayKaparuwan> do not return in your finally clause. Then when you raise an error, the finally clause will run, and your error will be raised after.
Perhaps deserving a separate question, I have the following use case for both return and raise. Function f1 iteratively queries an API endpoint, each request may result in an exception. Once it stopped (normally or due to exception), it should return the accumulated responses for processing to the caller, f0. The caller and the outer context want to know of certain exceptions occurred. Maybe I got my code architecture completely wrong, but I ended up handling certain exception in f1, returning them to f0 and re-raising there.
@paperskilltrees: This is a normal use case for the asyncio stuff, and the solution there is that they return the actual exception objects along with the results for the things that don't fail. They also take flags to determine if that should be the behavior, or if it should raise the first exception encountered, etc.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.