How to write try statement in python

Python's try statement has two styles

One is to handle exceptions (try/except/else)

The second is that the final code will be executed regardless of whether an exception occurs (try/finally)

try/except/else style

try:<Statement#Running code
except<first name:
< Statement#If it is triggered in the try part'name'abnormal
except<first name,<data:<Statement#If it triggers'name'Exception, get additional data
else:<Statement#If no exception occurs

The working principle of try is that when a try statement is started, python marks it in the context of the current program, so that when an exception occurs, you can come back here. The try clause is executed first, and what happens next depends on the execution time. Whether there is an abnormality.

1、 If an exception occurs when the statement after the try is executed, python jumps back to the try and executes the first except clause that matches the exception. After the exception is processed, the control flow passes through the entire try statement (unless a new Exception).

2、 If an exception occurs in the statement after the try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top of the program (this will end the program and print the default error message).

3、 If no exception occurs during the execution of the try clause, python will execute the statement after the else statement (if there is an else), and then control flow through the entire try statement.

try/finally style

try:<Statement
finally:<Statement#Always execute when exiting try

Python will always execute the finally clause, regardless of whether an exception occurs when the try clause is executed.

1、 If no exception occurs, python runs the try clause, then the finally clause, and then continues.

2、 If an exception occurs in the try clause, python will return to execute the finally clause, and then submit the exception to the upper try, and the control flow will not pass through the entire try statement.
Try/finally is useful when you want to ensure that certain code is executed regardless of whether an exception occurs.

This is useful when opening a file finally always close() the file at the end

**Try statement clause form **

try:
f=open('file.txt')
exceptIOError,e:
printe
else:
print'wrong'
[ Errno2]Nosuchfileordirectory:'file.txt'

The latest python version supports try/except/finally

1: If x is not abnormal, execute z, i

2: If x is abnormal:

One: if except catches an exception, execute y, i

Two: not caught, execute i, and then return to built-in exception handling

try:
x
except(name):
y
else:
z
finally:
i

Recommended Posts

How to write try statement in python
How to write classes in python
How to write return in python
How to write win programs in python
Teach you how to write games in python
How to write a confession program in python
How to write python configuration file
How to wrap in python code
How to omit parentheses in Python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to run id function in python
How to install third-party modules in Python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
print statement in python
Write gui in python
How to use the zip function in Python
How to program based on interfaces in Python
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
How to delete files and directories in python
How to install the downloaded module in python
How to read and write files with Python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
python how to view webpage code
How to use hanlp in ubuntu
Use C++ to write Python3 extensions
How to install PHP7.4 in CentOS
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8