Exception handling is processing that prevents the program from being interrupted due to an error occurring in the middle. For example, try the following:
list = [1,2,3,4,'a',5,6]
for i in list:
print(i/10)
Then
0.1
0.2
0.3
0.4
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-78c5fd70e082> in <module>
2
3 for i in list:
----> 4 print(i/10)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
An error occurs due to the "a" notation, and the program is interrupted. Therefore, an error is displayed.
for i in list:
try:
print(i/10)
except:
print("Error")
0.1
0.2
0.3
0.4
Error
0.5
0.6
I did well.
Recommended Posts