Advanced Python exception handling road

Disclaimer: This tutorial learning exchanges only, not for commercial use. https://blog.csdn.net/weixin_45086637/article/details/91403741

Exception Handling

So far, the program has encountered an error in Python, or "abnormal", thought the whole program crashes. You do not want this happen in the real world in the program, on the contrary, you want the program to be able to detect errors, process them, and then continue to run.

For example, consider the following program, it has a "divide by zero" error. Enter the code as follows:

def spam(divideby):
    return 42 / divideby

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))

Output:

21.0
3.5
Traceback (most recent call last):
  File "/Users/limingda/Desktop/\u684c\u9762\u6587\u4ef6/python/python1/test.py", line 6, in <module>
    print(spam(0))
  File "/Users/limingda/Desktop/\u684c\u9762\u6587\u4ef6/python/python1/test.py", line 2, in spam
    return 42 / divideby
ZeroDivisionError: division by zero

We have defined a function called spam, giving it one argument, and then print out the value of the function with various parameters. When trying to use a number by zero, ZeroDivisionError occurs. According to aircraft flight information given in error, we know that spam () in the return statement causes an error.

As a function of the "black box", generally, for a function, you need to know that it is an input value (argument) and output values. You do not always need to increase the burden on their own, the code to figure out the actual function of how it works. If such a high-level way of thinking about function, we usually say that you as the function is a black box.

This idea is the basis of modern programming. Later in this chapter will show you some of the modules, the function of which is written by someone else. Despite the noble spirit of the time you can take a look at the source code, but in order to use them, you do not need to know how they work. Moreover, because not encourage the use of global variables when writing function, you usually do not have to worry about the rest of the code and the program will occur as a function of cross-impact.


Errors can have try and except statements to deal with. Those statements may be wrong in a try clause. If an error occurs, program execution passes to the next at the beginning except clause.

Division by zero in front of the code can be placed in a try clause, so that except clause contains code to handle error occurs when the right thing to do.

def spam(divideby):
    try:
        return 42 / divideby
    except ZeroDivisionError:
        print('Error:Invalid argument.')

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))

If you try clause causes an error in the code, program execution immediately go to the Code except clause. After what code is running, execution continues as usual. Running the above code is output as follows:

21.0
3.5
Error:Invalid argument.
None
42.0

Please note that all errors in the try block function call, happens to be captured. Consider the following program, its practice is not the same, the spam () call in the statement block:

def spam(divideby):
        return 42 / divideby
  
try:
    print(spam(2))
    print(spam(12))
    print(spam(0))
    print(spam(1))
except ZeroDivisionError:
    print('Error:Invalid argument.')

Output:

21.0
3.5
Error:Invalid argument.

print (spam (1)) is never executed because, once the code except clause to perform, will not be back to try clause. It will continue as usual will continue.

Guess you like

Origin blog.csdn.net/weixin_45086637/article/details/91403741