Exception handling in python (code example)

不言
Release: 2019-01-25 10:02:47
forward
2445 people have browsed it

The content this article brings to you is about exception handling (code examples) in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

When we think that some code may go wrong, we can use try to run this code. If an error occurs, the subsequent code will not continue to execute, but will jump directly to the error handling code, that is except statement block, after executing except, if there is a finally statement block, the finally statement block will be executed. At this point, the execution is completed.

Python’s try statement has two styles

  1. Handling exceptions (try/except/else)

  2. The first is that the last code (try/finally) will be executed regardless of whether an exception occurs.

try/except/else style

try:
    <语句> #运行别的代码
except <名字>: 
    <语句> #如果在try部份引发了&#39;name&#39;异常
except <名字>,<数据>:
    <语句> #如果引发了&#39;name&#39;异常,获得附加的数据
else:
    <语句> #如果没有异常发生
Copy after login

try The working principle 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 return here. The try clause is executed first, and what happens next depends on whether it occurs during execution. abnormal.
1. If an exception occurs when the statement after try is executed, python will jump back to try and execute the first except clause matching the exception. After the exception is handled, the control flow will pass through the entire try statement (unless processing A new exception is thrown when an exception occurs).
2. If an exception occurs in the statement after try, but there is no matching except clause, the exception will be submitted to the upper try, or to the top level of the program (this will end the program and print the default error message).
3. If no exception occurs when the try clause is executed, python will execute the statement after the else statement (if there is an else), and then the control flow passes through the entire try statement.

try/finally style

try:
    <语句>
finally:
    <语句> #退出try时总会执行finally语句
Copy after login

Python will always execute the finally clause, regardless of whether an exception is thrown 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 come back to execute the finally clause, and then submit the exception to the upper try. 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 table
except: catch all exceptions
except name: catch only Specific exceptions
except name,value: Catch the exception and its additional data (save the exception information to value,)
except (name1,name2): Catch any listed exceptions
else: if No exception
finally: always executed

try:
   f = open(&#39;file.txt&#39;)
except IOError as e:
   print e
else:
   print &#39;wrong&#39;
Copy after login
[Errno 2] No such file or directory: &#39;file.txt&#39;
Copy after login

try/except/finally:

#1:如果x没有异常,执行z,i
#2:如果x有异常, 
#一:如果except捕捉到异常则执行y,i
#二:没捕捉到,执行i,然后返回内置异常处理 

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

The above is the detailed content of Exception handling in python (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template