当前位置:网站首页>[basic 7] - exceptions, capture and customize exceptions
[basic 7] - exceptions, capture and customize exceptions
2022-07-24 05:09:00 【terrific51】
abnormal
Exceptions will cause the program to terminate
One 、 Common abnormal
SyntaxError Grammar mistakes NameError Wrong variable name IndentationError The indentation error ModuleNotFoundError Third party package not installed FileNotFoundError File not found AttributeError: 'int' object has no attribute 'eat' Object does not have a property or method IndexError: list index out of range Index out of bounds ZeroDivisionError: division by zero The divisor cannot be zero 0
Two 、 Capture exception
When an exception occurs , Get abnormal information and deal with it accordingly , Avoid program crashes
onlyCan catch runtime exceptions , If the code itself does not work properly, it cannot be captured
try···except
try:
Code with possible exception
except Exception as e:
Code executed when an exception occurs
lista=[15,15,45,8,0,98,8,14]
for s in lista:
try:
r=10/s
print(r)
except Exception as e:#Exception The type of error captured e Save the specific error content
print(" There is an error :",e)
0.6666666666666666
0.6666666666666666
0.2222222222222222
1.25
There is an error : division by zero
0.10204081632653061
1.25
0.7142857142857143
else and finally sentence
try:
Code with possible exception
except Exception as e:
Code executed when an exception occurs
else:
Code executed when there is no exception
finally
Whether or not there is an exception , It will be carried out ( Log , close resource )
lista=[15,15,45,8,0,98,8,14]
for s in lista:
try:
r=10/s
print(r)
except Exception as e:#Exception The type of error captured e Save the specific error content
print(" There is an error :",e)
else:
print(" normal !")
finally:
print("---- completion of enforcement !")
0.6666666666666666
normal !
---- completion of enforcement !
0.6666666666666666
normal !
---- completion of enforcement !
0.2222222222222222
normal !
---- completion of enforcement !
1.25
normal !
---- completion of enforcement !
There is an error : division by zero
---- completion of enforcement !
0.10204081632653061
normal !
---- completion of enforcement !
1.25
normal !
---- completion of enforcement !
0.7142857142857143
normal !
---- completion of enforcement !
3、 ... and 、 Custom exception
Create an exception object
exp=Exception(" Gender can only be male or female !")# Create an exception object
sex=input(" Please enter your gender ( male 、 Woman )")
if sex==" male "or sex==" Woman ":
print(" Gender you entered :",sex)
else:
print(" The gender you entered is wrong !")
raise exp
Please enter your gender ( male 、 Woman )wu
The gender you entered is wrong !
Exception: Gender can only be male or female !
边栏推荐
- Why can't I hide folders? Solutions to the hidden folders on the computer that can still be seen
- 招聘| 嵌入式軟件(单片机)工程师
- What is the sandbox technology in the data anti disclosure scheme?
- This article takes you to understand C string functions and memory functions in simple terms
- Hcip-- review the homework for the next day
- IDEA:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
- 连接数%的准确率。现在拟需求。企业在数足以
- MySQL transaction and its problems and isolation level
- 口叫SC 或者 pb 文件为读写控制ensor为
- Jetson设备 faild to download repository information使用小技巧记录
猜你喜欢

一文带你深入浅出C字符串函数和内存函数

Uniapp learning

Context encoders: feature learning by painting paper notes

frp内网穿透服务使用

浅谈不可转让的声誉积分NFT SBTs面临的困境
![Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]](/img/1e/95078ad64a17686463547e8ba87cb1.png)
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]

Kingbase V8R6集群安装部署案例---脚本在线一键扩容

Redis enhancements

智能指针、左值引用右值引用、lambda表达式

Chapter 9 using image data
随机推荐
Fiddler抓包工具的使用
13. Write a program, in which a user-defined function is used to judge whether an integer is a prime number. The main function inputs a number and outputs whether it is a prime number.
Chapter 7 other neural network types
Mysq Database Constraints
Post SQL era: edgedb 2.0 Release Notice
How to get the signature file of Baidu Post Bar? Baidu Post Bar signature file setting and use method graphic introduction
Event extraction and documentation (2020-2021)
[machine learning] - [traditional classification problem] - naive Bayesian classification + logistic regression classification
PostgreSQL: run PostgreSQL + pgadmin 4 in docker
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
网NN计算能主机系统资e提供的NTCP
Format problem handling
Pony activation tool appears cannot open file k:\oemsf solution
Foreign key operation of MySQL_ Cascade operation
Update C language notes
Jiang Xingqun, senior vice president of BOE: aiot technology enables enterprise IOT transformation
What does the red five pointed star in the lower right corner of sina Weibo avatar mean? How to become a master of sina Weibo?
Introduction to MapReduce
Sword finger offer special assault edition day 7
Summary of common errors in wechat applet cloud development