try-finally statements
The try-finally statement executes the final code regardless of whether an exception occurs.
Try:< statements >finally:< statement >< Span class= "PLN" style= "margin:0px; padding:0px; border:0px CurrentColor; Color: #000000; " > #退出try时总会执行 Span class= "PLN" style= "margin:0px; padding:0px; border:0px CurrentColor; Color: #000000; " >raise
(
Raise
We can use the raise statement to trigger the exception raise syntax format as follows:
Raise [exception< Span class= "PLN" style= "margin:0px; padding:0px; border:0px CurrentColor; Color: #000000; " > [, args [, Traceback
)
#-*-Coding:utf-8-*-Try:Fh=Open( "testfile" , try: Fh. ( "This is a test file for testing exceptions!!" finally: "close file" Fh. () except ioerror : print "Error: Failed to find file or read file "
Executes the finally block code immediately when an exception is thrown in the try block.
After all the statements in the finally block are executed, the exception is triggered again, and the except block code is executed.
The contents of the parameter differ from the exception.
Python Exception handling