This kind of statement is a multi-processing method of multiple except statements. Let's learn the nesting of try...except statements through an example.
deftest():n=int(input('There are 3 statements to test, please select (1/2/3):')) ifn==1: #keyError exception my_dict={'dotcpp':123}print (my_dict['dot'])elifn==2:#IndexError exception my_list=[1,2,3,4,5]print(my_list[100])eli fn==3:#NameError exception print('non-existent variable dotcpp:',dotcpp)try:try:try:test()exceptKeyError:print('KeyError exception')exceptIndexError:print('IndexError exception')exceptNameError: print('NameError exception')
We tested 1/2/3 respectively, and the output results are as follows:
There are 3 statements to be tested, please select (1/2/3): 1KeyError exception There are 3 statements to be tested, please select (1/2/3): 2IndexError exception There are 3 statements to be tested, please select (1/ 2/3): 3NameError exception
In this example, we first enter a judgment n in the defined function, and then give three exception situations based on the value of n, and then we nest three try..except statements at the end, that is to say The result of the test() statement will go through three levels of judgment.
A complete exception handling mechanism is inseparable from the finally statement . The function of this statement is that regardless of whether an exception occurs in our program, the code under the finally statement will be executed. Its syntax format is:
try:blockexcept[typeerror]:deal1finally:deal2
Let's take a look at this structure through an example:
deftest():n=int(input('Please enter an integer:'))print(n)try:test()exceptKeyError:print('KeyError exception')finally:print('finally statement has been executed')
Let’s enter 6 and dotcpp respectively to see the output:
Please enter an integer: 55finally statement has been executed Please enter an integer: dotcppfinally statement has been executed Traceback(mostrecentcalllast):FileC:/Users/test.py,line5,in<module> test()FileC:/Users/test.py,line2,intestn=int(input('Please enter an integer:'))ValueError: invalidliteralforint()withbase10:'dotcpp'
From this example we can see that the finally statement will be executed regardless of whether an exception occurs in our program.
The finally statement we learned above is a statement that will be executed regardless of whether an exception occurs in the statement. The try...except...else statement we will learn below will only be executed when no exception occurs in the program. Its syntax structure is :
try:blockexcept[typeerror]:deal1else:deal2
Let's look at an example below:
deftest():n=int(input('Please enter an integer:'))print('The input value is:',n)try:test()exceptValueError:print('ValueError exception')else:print('Current No exception occurred in the program')
The output is:
Please enter an integer: 5. The input value is: 5. There is no exception in the current program.
When we enter the value dotcpp
Please enter an integer: dotcppValueError exception
We can see that the else statement is not executed when the program is abnormal. It is not difficult to distinguish these statements. You can practice these commonly used exception handling statements through examples.