finally and rethowing of exception in except, raise in python

前端 未结 1 974
孤独总比滥情好
孤独总比滥情好 2021-02-12 07:53
try:
   #error code
except Exception as e:
   print \'error\',e
   raise miexp(\"malicious error\")  
   #userdefined exception, miexp
finally:
   print \'finally\'
         


        
1条回答
  •  梦谈多话
    2021-02-12 08:38

    miexp("malicious error") isn't handled, therefore it will end the execution of the program. On the other hand, the finally block is guaranteed to be executed.

    To ensure this Python executes the finally block before actually raising the exception. From the documentation:

    If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause.

    0 讨论(0)
提交回复
热议问题