I want to get out of a function when an exception occurs or so. I want to use other method than 'return'
Using return is the easiest way to exit a function. You can use return by itself or even return a value.
The return keyword is to exit a function and return a value.
Assuming you want to "stop" execution inside of that method. There's a few things you can do.
If you catch an exception and then want to rethrow it, this pattern is pretty simple:
try:
do_something_dangerous()
except:
do_something_to_apologize()
raise
Of course if you want to raise the exception in the first place, that's easy, too:
def do_something_dangerous(self):
raise Exception("Boo!")
If that's not what you wanted, please provide more information!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With