Python Exception handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.