The use of try...except...finally in Python

Source: Internet
Author: User

The use of try...except...finally in Python

Author:headsen Chen

date:2018-04-09 16:22:11

Try, except, finally is the exception-catching mechanism in python, and the usual usage is try: Except ... In combination, the program captures the exception in the TRY statement block, if the exception is found to the exception to the except in the statement block for processing, that is, the execution of except statements, here except can also be combined
If...else used together.


def read_file ():
    Try:
        Print (' AAA ')
    except :
        print (' Error occurs while reading file ')
    Finally:
        Print (' bbbb ')
read_file ()

------>

Aaa
bbbb
Summary: Run as soon as the statement below the try runs correctly. Run the following statement after running Finaly

Example of an exception in 2:try:

When the statement below the try is run, when the exception is encountered, run except the following statement, and finally run finaly the following statement, finaly statements generally do some resources to release the work, such as closing open files.

def read_file ():
    try:
        print (2222222222)
        Print (AAA)
    except:
        Print (' error occurs while reading file ')
    finally:
        print (' bbbb ')
read_file ()

------>

2222222222 ----------> proves that the correct program in the try is executed, and the exception is executed after it is transferred to the except section.
Error occurs while reading file
bbbb

Of course, try...except can also be used in combination with finally. Then finally in the end, the content of the finally statement block is usually done for some funeral processing, such as the release of resources, and finally statement blocks are executed anyway, even in the previous try and except statement block in the return, are now
Executes the finally statement and then executes the preceding return statement. Let's look at a simple example:

Example 3: the use of Try...except...else

Else only in the try below the statement is executed correctly will execute else,try inside there is an exception will not else statement, and finaly different, finaly is regardless of whether there is no exception in the try to execute

Def read_file ():
Try
Print (2222222222)
Except
Print (' error occurs while reading file ')
Else
Print (' bbbb ')
Read_file ()


Example 4: Take the return value 1

When the return value in the exception function is evaluated: The return value of the entire function takes the return value of the try as it is executed correctly

def read_file ():
Try:
print (2222222222)
# Print (a)
return 5
except:
print (' error occurs while reading file ')
return 6
finally:
print (' bbbb ')
print (Read_file ())

----->

2222222222
bbbb
5

Example 5: Take the return value 2

If there is an exception in the try, the return value of the whole function takes the return value of except

def read_file ():
Try:
print (2222222222)
print (a)
return 5
except:
print (' error occurs while reading file ')
return 6
finally:
print (' bbbb ')
print (Read_file ())

----->

2222222222
Error occurs while reading file
bbbb
6

The use of try...except...finally in Python

Related Article

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.