Finally and revising exception to exception, raising in python

try: #error code except Exception as e: print 'error',e raise miexp("malicious error") #userdefined exception, miexp finally: print 'finally' 

Why is the output in the following formats?

Output:

 error finally malicious error 

I actually expected that:

 error malicious error finally 

Why is that?

+5
source share
1 answer

miexp("malicious error") not processed, so the program terminates. On the other hand, the execution of the finally block is guaranteed.

So that this Python executes the finally block before actually throwing an exception. From the doc:

If an exception occurs in any of the sentences and is not processed, the exception is temporarily saved. The finally clause is executed. If there is a stored exception, it will be raised again at the end of the finally clause.

+7
source

Source: https://habr.com/ru/post/1238005/


All Articles