If there's an exception, the code in the corresponding “except” block will run, and then the code in the “finally” block will run. If there are no exceptions, the code in the “else” block will run (if there's an “else” block), and then the code in the “finally” block will run.
The raise keyword raises an error and stops the control flow of the program. It is used to bring up the current exception in an exception handler so that it can be handled further up the call stack.
except block is completed and the program will proceed. However, if an exception is raised in the try clause, Python will stop executing any more code in that clause, and pass the exception to the except clause to see if this particular error is handled there.
I'm trying to catch an exception and raise a more specific error at a point in my code:
try:
something_crazy()
except SomeReallyVagueError:
raise ABetterError('message')
This works in Python 2, but in Python 3, it shows both exceptions:
Traceback(most recent call last):
...
SomeReallyVagueError: ...
...
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
...
ABetterError: message
...
Is there some way to get around this, so that SomeReallyVagueError
's traceback isn't shown?
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