程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python note 22 exception mechanism

編輯:Python

abnormal

While the program is running , Due to poor coding or some other reason , Cause the program to not work properly , This means that the program has an exception

Normally, exceptions are not handled , The program will be suddenly interrupted due to an exception

In order to ensure the robustness of the program , So we have the concept of exception handling in programming

exception handling

It is often called catching exceptions

Abnormal process : A corresponding exception object will be thrown where an exception occurs , The interpreter will detect whether there is any operation near the code to handle the exception , If not, the interpreter will handle the exception directly , The end result is that the program is interrupted

grammar

try:
There may be unexpected code
except The type corresponding to the exception :
Handle the exception accordingly
except The type corresponding to the exception 2:
Handle the exception accordingly

When try When there are multiple exceptions in the block that may occur , Encounter the first burst of abnormality ,try The code of the block ends directly , To execute corresponding except Processing code , If there is no corresponding except Handle , The program will be interrupted again

Each exception has a corresponding description when it occurs , Get the description of the corresponding exception

try:
There may be unexpected code
except The type corresponding to the exception as Variable name :
Handle the exception accordingly
except The type corresponding to the exception 2 as Variable name :
Handle the exception accordingly

The variable name will receive the description of the corresponding exception

Simplification of the above code

try:
There may be unexpected code
except ( The type corresponding to the exception , Exception types 2, Exception types 3) as Variable name :
Handle the corresponding exception

Other keywords about exception handling else and finally

else

The application in the branch is when if When the judgment condition of is not satisfied perform else The operation

try:
There may be unexpected code
except ( The type corresponding to the exception , Exception types 2, Exception types 3) as Variable name :
Handle the corresponding exception
else:
When try Execute when there is no exception in the block else The operation

Case study : Enter an integer , Judge whether the data is even

finally

try:
There may be unexpected code
except ( The type corresponding to the exception , Exception types 2, Exception types 3) as Variable name :
Handle the corresponding exception
finally:
Whether there are exceptions or not, it will execute

It is often used in scenarios where resources are released

Case study : When it comes to document processing , The connection between program and file is closed , Release the resource of the file used by the program

The transmission of exceptions

When defining a function , There may be some exceptions in this function , But there is no processing in the code , Called function A An exception may be triggered when , If the call is not processed , Call the next level B, stay B Used in A, This exception is passed to B The location of

Throw an exception

Throw an exception manually

raise Exception types ( describe )

Custom exception types

All exception types are derived from BaseException, Is the root class of all exception types ,

Some common exception types are inherited from Exception

If you define a type as an exception type , You need to make this class inherit from Exception

Building triangles , Set up 3 Sides long , The condition is that the sum of any two sides is greater than the third side


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved