Learned about Python's error handling today.
In the process of program processing, there are often errors, once the error is very troublesome. So some high-level languages are usually built into a set of try...exept...finaly ... Error-handling mechanism, Python is no exception.
Here's an example of a try:
try:print (10/0 print (except zerodivisionerror as e:print ( ' except: ', E) finally:print ( finally ... ') print (
Because 10/0 is wrong, this time the try this part will execute the error, then the subsequent code will not execute, will jump directly to the error handling code, that is, the except statement block, after execution if there is a finally statement block to run finally, this concludes.
try...except: division by zerofinally...END
由此可以看出r = 10/0后面的print(‘result:‘,r)没有运行，反而except捕捉到了错误所以输出
division by zero
Finally, the finally is executed.
If you change the "R = 10/0 '" of the program above to "R = 10/5"
The output will be:
try:print (10/int ( ' a ') print (except valueerror as e:print ( ' ValueError: ', e) except zerodivisionerror as e:print (finally:print ( Int () The function may execute valueerror, so the first except will catch the wrong value, the other except capture the zerodivisionerror, and if there is no error we can
add an else in the back, and no error will execute the ELSE statement.
try:print (10/int ( ' 2 ') print (except valueerror as e:print ( ' ValueError: ', e) except zerodivisionerror as e:print (else:print ( ' no error! ') finally:
python error is also class, all the error types inherit from Baseexception, there may be inheritance between the various types of errors, For example, Unicodeerror is a valueerror subclass,
Deffoo (s): return 10/int (s) Span class= "function" >def bar (s): return foo (s) * 2def main (): try:bar ( ' 0 ') except Exception as e:print ( Finally:print (
that is, there is no need to catch errors at each error, Just catch the error at the right level. In this way, it greatly reduces the write try...except...finally ... of trouble.
If the error is not captured, he throws it up, preferably caught by the Python interpreter, prints an error message, and the program exits. See err.py:
# err.py:< span class= "keyword" >def foo (s): return 10/int (s) def bar (s): return foo (s) * 2def Span class= "title" >main (): Bar ( ' 0 ') main ()
< span class= "keyword" >< Span class= "params" >< Span class= "title" > execution results are as follows:
$ python3 err.pyTraceback (most recent call last): File "err.py", line 11, in <module> main() File "err.py", line 9, in main bar(‘0‘) File "err.py", line 6, in bar return foo(s) * 2 File "err.py", line 3, in foo return 10 / int(s)ZeroDivisionError: division by zero
It 's not scary to make an error. Interpreting information is the key to locating errors. We can see the entire wrong call function chain from the top down:
error message First line:
Tell us that this is the wrong tracking information.
Line 2nd to 3rd:
The call to main () is wrong, and the 11th line of code in the code file err.py, but the reason is line 9th:
File "err.py", line 9, in main bar(‘0‘)
The call to bar (0) went wrong, the 9th line of code in the code file err.py, but the reason is line 6th :
File "err.py", line 6, in bar return foo(s) * 2
< span class= "keyword" >< Span class= "params" >< Span class= "title" >
file "err.py" , line 3, in foo return 10/ Int (s)
< Span class= "params" >< Span class= "title" >
ZeroDivisionError: integer division or modulo by zero
Python Error handling try...except...finally ... Error handling mechanism.