As the saying goes, there are unforeseen circumstances. When we write programs, it cannot always be smooth sailing. Some abnormal situations often occur. For example, when we are going to go out to watch a movie, we first go out, then walk to the cinema, and then finish watching. We went shopping after watching a movie and walked back home. However, after watching the movie, we found that it was raining outside. At this time, we had to cancel our shopping plan and return home. The rain here is an abnormal situation, so we designed the program It's time to add this exception handling solution.
Let's first look at an exception problem . The code is as follows:
number=int(input('Please enter a number:'))if15/number>=3:print('ok')else:print('wrong')
Output result:
Please enter a number: 3ok Please enter a number: 15wrong Please enter a number: 0Traceback(mostrecentcalllast):FileC:/Users/test.py,line2,in<module>if15/number>=3:ZeroDivisionError:divisionbyzero
When the denominator we input is 0, we will find that the program reports an error, which means there is a problem with the program we wrote. Because we did not consider what to do when the denominator is 0, an exception occurred in the program, so we wrote the program You need to take into account some abnormal situations that may occur. Let's learn about several common abnormal situations.
This is an exception that occurs when accessing a dictionary key and the key value does not exist.
The code is as follows:
my_dict={'dotcpp':123}print(my_dict['dot'])
The exception is as follows:
Traceback(mostrecentcalllast):FileC:/Users/test.py,line2,in<module>print(my_dict['dot'])KeyError:'dot'
This kind of exception is more common when we learn Python. When we access sequence elements , this exception mechanism will be triggered when the index value exceeds the range. The code is as follows:
my_list=[1,2,3,4,5]print(my_list[100])
The exception is as follows:
Traceback(mostrecentcalllast):FileC:/Users/test.py,line2,in<module>print(my_list[100])IndexError:listindexoutofrange
There are only 5 elements in our list, and this exception will be thrown if the access exceeds the limit.
This exception is thrown when we use an undefined variable , the code is as follows:
name=['www.dotcpp,com']print('Existing variable name:',name)print('Non-existing variable dotcpp:',dotcpp)
The exception is as follows:
Existing variable name:['www.dotcpp,com']Traceback(mostrecentcalllast):FileC:/Users/Qingyan/PycharmProjects/untitled1/teach/test.py,line3,in<module>print('Non-existing variable dotcpp:',dotcpp)NameError:name'dotcpp'isnotdefined
This exception occurs when the data passed is inconsistent with the specified type. For example, when we want to add and subtract two integers, and one of them uses a string type, this error will occur.
The code is as follows:
a=10b='dotcpp'print(a+b)
The exception is as follows:
Traceback(mostrecentcalllast):FileC:/Users/test.py,line3,in<module>print(a+b)TypeError:unsupportedoperandtype(s)for+:'int'and'str'
The above are some of the most common exceptions. We may encounter ValueError, OSError, AttributeError and other exceptions in the process of learning. When we encounter an exception we don’t recognize, we can query the relevant content through the help document.