I'm learning to use python. I just came across this article: http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html It describes rethrowing exceptions in python, like this:
try: do_something_dangerous() except: do_something_to_apologize() raise
Since you re-throw the exception, there should be an "outer catch-except" statement. But now, I was thinking, what if the do_something_to_apologize()
inside the except throws an error. Which one will be caught in the outer "catch-except"? The one you rethrow or the one thrown by do_something_to_apologize()
? Or will the exception with the highest priotiry be caught first?
Python Language Exceptions Re-raising exceptions In this case, simply use the raise statement with no parameters. But this has the drawback of reducing the exception trace to exactly this raise while the raise without argument retains the original exception trace.
The purpose of the rethrow operation is to get the attention of the outside world that an exception has occurred and at the same time perform any contingency logic (such as logging) in the catch block.
No of course not. There are cases when comment is still needed because the programming languages are not always best suited to describe what is going on very well.
Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.
Try it and see:
def failure(): raise ValueError, "Real error" def apologize(): raise TypeError, "Apology error" try: failure() except ValueError: apologize() raise
The result:
Traceback (most recent call last): File "<pyshell#14>", line 10, in <module> apologize() File "<pyshell#14>", line 5, in apologize raise TypeError, "Apology error" TypeError: Apology error
The reason: the "real" error from the original function was already caught by the except
. apologize
raises a new error before the raise
is reached. Therefore, the raise
in the except
clause is never executed, and only the apology's error propagates upward. If apologize
raises an error, Python has no way of knowing that you were going to raise a different exception after apologize
.
Note that in Python 3, the traceback will mention both exceptions, with a message explaining how the second one arose:
Traceback (most recent call last): File "./prog.py", line 9, in <module> File "./prog.py", line 2, in failure ValueError: Real error During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./prog.py", line 11, in <module> File "./prog.py", line 5, in apologize TypeError: Apology error
However, the second exception (the "apology" exception) is still the only one that propagates outward and can be caught by a higher-level except
clause. The original exception is mentioned in the traceback but is subsumed in the later one and can no longer be caught.
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