Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise exception in except block and suppress first error [duplicate]

Tags:

People also ask

Can we raise exception in except block?

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.

What is the use of raise exception?

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.

Does increasing exception stop execution?

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?