Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we return after raise statement

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
like image 704
Pranjay Kaparuwan Avatar asked Nov 04 '16 05:11

Pranjay Kaparuwan


1 Answers

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.

like image 122
ShadowRanger Avatar answered Oct 28 '22 12:10

ShadowRanger