Last Updated on 2021-04-10 by Clay
The process of coding, it always have some bug.
Sometimes, we maybe think about any situations we can consider, but the wrong always happening. It is even have wrong happening after we execute the program for a long running time. And we don't know!
It is a pity that the time wasted.
Of course, this happens will let the progress of our project slowed down. Therefore, it is quite important to consider "error" and avoid it when we coding.
Our notes is it. We will talking about the "try" and "except" in Python.
When we execute the program, we can place the code in the "try" statement, and then when the error occurs, we will continue to execute from the "except" statement.
The boring deception is over. Let's take a look at the actual code!
Try & Except
We take a look for an error.
for n in range(10) print(n)
Output:
for n in range(10) print(n)
^
SyntaxError: invalid syntax
The above is a simple error!
We say it's simple is so strange, however, many people forget to use ":" to separate "for-loop" and "code to execute" when they using for-loop.
We fixed this BUG:
for n in range(10): print(n)
Output:
0
1
2
3
4
5
6
7
8
9
Fixed a bug we have error, and Python code started executing normally.
But we have to face a real problem: If we meet a bug in the processing of developing a program, can we ignore the cost? (for example, staying up late, the boss is not happy by the software development progress.). So, I will not be able to meet it. Did the execution scenario find a particular problem?
It's time to use "try" and "except"!
Let us look at a simple example:
try: hello() except: print("It's wrong!")
Output:
It's wrong!
Here first explained: hello() is a function we not defined in the code before, and then we call it.
It will have error in our program running. But this time, we use try and except to avoid the problem! The error don't interrupt our program, in stead of executing the code in the "except" statement.
Application
Suppose we are performing an automated task, such as a stock crawler (about what is a "crawler", may not be too familiar with some beginner, so I will briefly introduction here: basically let the program go online, and download the required web page information.)
At this time, we just caught a web page that was dead, and even the connection refused, causing our crawler to make mistakes.
This time try and except is a very convenient tool that allows us to skip a page that will cause an error.
In addition, try and except allows us to use the keyboard to interrupt our program to leave the error display is relatively clean.
try: for n in range(99999999): print(n) except KeyboardInterrupt: print('GoodBye. My Lord.')
Output:
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
GoodBye. My Lord.
I pressed ctrl + c
to interrupt the program when the program was executed until 1726 was printed. Originally the program will generate a lot of error messages, and the larger the program, the more cluttered it is.
If you use except to catch this type of error, you can keep your terminal clean.
Welcome to use try and except on your own project! This definitely has a lot of help in development!
Discussion
In addition to instructions such as try and except, there are still various instructions such as final, raise, etc. To help us deal with error messages and even throw errors —— there are very useful functions, but in general, we all you need to actually meet it to know how easy it is to use these features.
I hope everyone can have fun in the programming! and complete all of your projects!
References
Read More
- [Python] Tutorial (1) Download, and print "Hello World"
- [Python] Tutorial (2) Additional, subtraction, multiplication, division, String, Variable
- [Python] Tutorial (3) for-loop, if-else
- [Python] Tutorial (4) break, pass, continue
- [Python] Tutorial (5) Boolean, True, False
- [Python] Tutorial (6) greater than, less than, equal to
- [Python] Tutorial (7) function
- [Python] Tutorial (8) function, default , *args, **kwargs
- [Python] Tutorial (9) lambda
- [Python] Tutorial (10) List, Array of Python
- [Python] Tutorial (11) Tuples, Sets, Dictionary
- [Python] Tutorial (12) Import package: time, datetime, math
- [Python] Tutorial (13) try & except
- [Python] Tutorial (14) os, read and write the file