当前位置:网站首页>Try... Except exception handling statement (6)
Try... Except exception handling statement (6)
2022-07-28 15:37:00 【WHJ226】
Catalog
While the program is running , We often encounter all kinds of mistakes , These mistakes are collectively called “ abnormal ”.
1. abnormal
| abnormal | describe |
| SyntaxError:invalid syntax | invalid syntax |
| NameError | An error caused by an attempt to access an undeclared variable |
| IndexError | The index is out of range of the sequence, causing an error |
| IndentationError | The indentation error |
| ValueError | The value passed in is wrong |
| KeyError | Requesting a nonexistent dictionary keyword caused an error |
| IOError | I / O error |
| ImportError | When import Statement cannot find the error caused by the module |
| AttributeError | Error raised when trying to access unknown object properties |
| TypeError | Errors caused by improper type |
| MemoryError | Out of memory |
| ZeroDivisionError | When the divisor is 0 Error caused by |
2. exception handling
2.1 try...except
When use , Put the code that may cause the exception in try In the block , Put the result of the treatment on except In the block , such , once try An error occurred in the code in the statement block , Will execute except Code in statement block ; But if try There are no errors in the code in the statement block , Will not perform except Code in statement block .
The syntax is as follows :
try:
block1
except[ExceptionName[as alias]]:
block2
#block1: Represents a block of code that may have errors
#ExceptionName[as alias]: Optional parameters , Used to specify the exception to catch .ExceptionName Indicates the name of the exception to catch ,as alias The alias that records the specific content of the exception .
#block2: Code block for exception handling for example :
Before processing :
def division(a,b):
result = a/b
print(result)
division(4,2)
division(4,0)The operation results are as follows :
2.0
Traceback (most recent call last):
File "E:\PYTHON\base4.py", line 25, in <module>
division(4,0)
File "E:\PYTHON\base4.py", line 21, in division
result = a/b
ZeroDivisionError: division by zeroThere is no exception after processing :
def division(a,b):
result = a/b
print(result)
if __name__ == '__main__':# Regulations , The main program , The following code is executed by default
try:
division(4,2)
print(' succeed , Divisor normal ')
except ZeroDivisionError:
print(' Something went wrong , The divisor cannot be zero 0')The operation results are as follows :
2.0
succeed , Divisor normal Catch exception after processing :
def division(a,b):
result = a/b
print(result)
if __name__ == '__main__':# Regulations , The main program , The following code is executed by default
try:
division(4,0)
print(' succeed , Divisor normal ')
except ZeroDivisionError:
print(' Something went wrong , The divisor cannot be zero 0')The operation results are as follows :
Something went wrong , The divisor cannot be zero 02.2 try...except...else
try...except...else sentence ,else Statement is used to specify when try Statement block to execute when no exception is found in statement block .
for example :
def division(a,b):
result = a/b
print(result)
if __name__ == '__main__':# Regulations , The main program , The following code is executed by default
try:
division(4,2)
except ZeroDivisionError:
print(' Something went wrong , The divisor cannot be zero 0')
else:
print(' succeed , Divisor normal , Deo gratias !')The operation results are as follows :
2.0
succeed , Divisor normal , Deo gratias !2.3 try...except...finally
If there is some code in the program that must be executed in any case , Then you can put it in finally In the code block of the statement .
for example :
def division(a,b):
result = a/b
print(result)
if __name__ == '__main__':# Regulations , The main program , The following code is executed by default
try:
division(4,2)
except ZeroDivisionError:
print(' Something went wrong , The divisor cannot be zero 0')
else:
print(' succeed , Divisor normal , Deo gratias !')
finally:
print('over')The operation results are as follows :
2.0
succeed , Divisor normal , Deo gratias !
over边栏推荐
- sql语句的执行流程
- 堆操作
- Configure CX Oracle solution (cx_oracle.databaseerror) dpi-1047: cannot locate a 64 bit Oracle client library: "th
- Matlab导出高清图片、且Word中压缩不失真、转换PDF不失真
- EasyExcel复杂表头导出(一对多)
- Grpc frequently asked questions
- Differences between two ways of QT creating folders
- MIT pointed out that the public pre training model should not be used indiscriminately
- Share the HR experience of the first and second tier companies
- Cross domain problems in the configuration of.Net core version 3.1
猜你喜欢
随机推荐
MIT pointed out that the public pre training model should not be used indiscriminately
简单入手Swagger
No files or folders found to process
使用Mock技术帮助提升测试效率的小tips,你知道几个?
Heuristic merging simple problem on tree
How many of the top ten test tools in 2022 do you master
Explain the difference set, intersection set and union set of complex type set in detail.Net
DAY:7/11
2. Self narration of open source GPS project hd-gr GNSS
游戏测试的概念是什么?测试方法和流程有哪些?
Differences between two ways of QT creating folders
Execution process of SQL statement
Editor in ArcGIS Pro
vs动态库调试
Daily question (retrospective)
解决pycharm使用powershell出错问题
Sharing of award-winning activities: you can get up to iphone13 after using WordPress to build your own blog
一篇文章了解RSocket协议
What functions will be added to crmeb Standard Version 4.4
Chrome plug-in debugging








