当前位置:网站首页>[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 !
边栏推荐
- Event extraction and documentation (2019)
- Want to know how a C program is compiled—— Show you the compilation of the program
- SHP building contour data restoration, 3D urban white film data production
- 1. There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8,... Program to sum the first 20 items of this sequence.
- PSO and mfpso
- )的低字节来反馈给应用层或者成多种格式文档:
- What if the computer can't take screenshots? The solution to the problem that the shortcut screen capture key of the computer cannot be used
- Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in
- 几种常见的排序
- Add.Eslinctrc.js under SRC for the general format of the project
猜你喜欢

Chapter V communication training

Middle aged crisis, workplace dad who dare not leave, how to face life's hesitation

frp内网穿透服务使用

Several common sorts

Event extraction and documentation (2020-2021)

Chapter III encog workbench

Redis enhancements

The difference between run and start in thread class

太空可再生能源的代币化

The world's first large aerospace model came out. Wenxin's second supplement "Fuchun Mountain Residence map" is Baidu Pratt Whitney AI's perseverance
随机推荐
MapReduce介绍
SHP building contour data restoration, 3D urban white film data production
This article takes you to understand C string functions and memory functions in simple terms
un7.23:如何在linix上安装MySQL?
MySQL constraint_ Foreign key constraint
Sort - quicksort
Hanoi problem
Emqx simple to use
Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in
GOM engine starts M2 prompt: [x-fkgom] has been loaded successfully. What should I do if it gets stuck?
Want to know how a C program is compiled—— Show you the compilation of the program
本,降低线上要度是一样的。发现异常实例cp操
Memorandum 2022
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
Binary SCA fingerprint extraction black Technology: go language Reverse Technology
Chiitoitsu(期望dp)
There is not enough space on the disk to complete this operation when partitioning the computer
Print leap years between 1000 and 2000
支撑复杂的模型群监控、实时告警等t4 文件系统。e
智能指针、左值引用右值引用、lambda表达式