2024-05-16 22:30:52 发布
网友
try: #error code except Exception as e: print 'error',e raise miexp("malicious error") #userdefined exception, miexp finally: print 'finally'
为什么输出采用以下格式?
输出:
error finally malicious error
实际上我希望是:
error malicious error finally
为什么?
miexp("malicious error")未被处理,因此它将结束程序的执行。另一方面,保证执行finally块。
miexp("malicious error")
finally
为了确保这个Python在实际引发异常之前执行finally块。从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.
miexp("malicious error")
未被处理,因此它将结束程序的执行。另一方面,保证执行finally
块。为了确保这个Python在实际引发异常之前执行
finally
块。从documentation:相关问题 更多 >
编程相关推荐