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

Python exception handling

編輯:Python

An exception is when the syntax is correct , An error when the program runs is an exception .

Catalog

Capture exception

Catch multiple exceptions

Custom exception


Capture exception

try...except...

try:
print("....")
except IOError:
pass

try In the middle is the code that may cause exceptions . When an error occurs , Jump to except Run seven methods to handle exceptions .

Catch multiple exceptions

If you want to pass once except Multiple exceptions can be caught in the form of a tuple

try:
print("...") # Code
except (IOError,NameError):
pass

Or handle different exceptions differently

try:
ptint("....")
except NameError:
print("nameerror")
except IOError:
print("IOError")

notes : The exception of the parent class should be placed after its child class , Otherwise, there will never be subclass exception handling

in addition , You can also use try...except...else sentence , When try When there is no exception in the statement in ,else The code in will be executed . You can also else Followed by finally,finally The statement in , No matter try It will be executed whether there is any exception in .

Custom exception

Custom exception Class inheritance since Exception class , Can inherit directly , Or indirectly .


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