Reference Python series serialization from scratch, by Wang Dawei Python enthusiast community
Refer to Hellobi Live | 1 hour icebreaker introduction to Python
Refer to "Concise Python Tutorial"
Note: For more serialization, please see [python]
content
exception handling
When the program runs and reports an error, an abnormal situation has occurred! Sometimes we want the system to provide more specific exception information to help us solve a problem. Sometimes we want to ignore this exception and let the program continue to execute.
So how do we control it?
In Python, we run the code that can throw exceptions in a 'quarantine' created using the try...except construct. The specific structure is as follows
try:
code that may cause an exception
except:
code for handling exceptions
The following code can catch all exceptions in try
try:
x = float(input('请输入被除数:'))
y = float(input('请输入除数:'))
z = x/y
print(z)
except:
print('出问题啦!')
Enter according to the prompt,
e.g.
请输入被除数:10
请输入除数:2
The result is
5.0
The above is a normal situation, the code part of try is fine, so the code part of the relative except will not be executed.
If input is as follows
请输入被除数:10
请输入除数:0
The result is a
problem !
First of all, the system does not report an error, because there is an error in the try, execute the content in the except, that is, print 'There is a problem! '
Of course, except will capture all possible errors in try, but there are many types of except exceptions , such as value exceptions, named exceptions , and so on. Since this is a division by 0, we can use a division by 0 exception to catch this exception.
ZeroDivisionError
Used to catch exceptions with division by 0
try:
x = float(input('请输入被除数:'))
y = float(input('请输入除数:'))
z = x/y
print(z)
except ZeroDivisionError:
print('除数不能为0')
1) When the input is
请输入被除数:10
请输入除数:0
The result is that the
divisor cannot be 0
No error was reported, the exception was successfully caught
2) When the input is
请输入被除数:10
请输入除数:a
The result will be an error
ValueError: could not convert string to float: 'a'
Failed to catch the exception, when the exception is not divided by 0, other kinds of exceptions cannot be caught with the exception of division by 0
ValueError
try:
x = float(input('请输入被除数:'))
y = float(input('请输入除数:'))
z = x/y
print(z)
except ZeroDivisionError:
print('除数不能为0')
except ValueError:
print('被除数和除数都应该是数值类型')
1) When the input is
请输入被除数:10
请输入除数:a
The result is that both the
dividend and the divisor should be of numeric type
Successfully catch exception
2) When input is
请输入被除数:10
请输入除数:0
The result is that the
divisor cannot be 0
Successfully caught exception
finally
try…except…finally…
When an exception occurs, we can use the capture method. If the exception is not caught, it will run an error, but sometimes, we must let some code run (regardless of whether there is an error in the previous run). At this time, in the original try... Add finally to the except... structure.
try:
z = 1/0
except ZeroDivisionError:
print('除了0')
finally:
print('我是一定会执行的')
The result is
except 0
, I will definitely execute
Successfully caught exception
try:
z = 1/0
except ValueError:
print('类型错误')
finally:
print('我是一定会执行的')
The result is
我是一定会执行的
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-7-fa23f8c24e0c> in <module>()
1 try:
----> 2 z = 1/0
3 except ValueError:
4 print('类型错误')
5 finally:
ZeroDivisionError: division by zero
Although the result will be wrong
ZeroDivisionError: division by zero
but
我是一定会执行的
still printed