Python/Try Statement
< Python
Objective![]()
|
LessonErrors and ExceptionsIn a perfect world, nothing goes wrong. Unfortunately, we don't live in a perfect world, so bad things are bound to happen. Computers can crash, monitors can short, and hard drives can corrupt. In the event that something goes wrong, purposely or not, the computer needs to be able to understand that something is going wrong and stop/fix the problem. Software also needs to be able to handle these problems, too. Errors happen when something doesn't go right. For example, pulling a CD out of a computer while burning something on it will result in the CD not being completely finished (corrupted) and it will also make the CD unavailable to the software using it (the CD burning program). If the program can't handle this problem it might display undefined behavior, like trying to continue burning to a nonexistent CD or the program could just hang. That's not very helpful to the user, so it's important to take care of these errors. Luckily, Python is able to handle errors and exceptions, unlike other programming languages. Python Errors vs. C ErrorsAs we've seen so far in this course, errors are very loud in Python. If something doesn't follow the rules or doesn't go right, Python will make sure you hear about it. Having every little mistake scrutinized without mercy might sound bad at first, but it actually becomes a vital necessity when you learn why. Early programming languages, like C, didn't come with built-in error handling. Sometimes errors went by unnoticed. Other times they caused the computer to crash. It was up to the programmer to create their own error handling support and even then it was still hard to catch errors in their tracks. Python's ultimate error handling goal is to let you know that an error has occurred. Having fulfilled its goal, what happens next is all up to you. If you don't specify anything to happen, then a default error message is displayed and the program is ended. Loud ErrorsNow that we have a bigger picture on errors, it becomes clear that handling errors is important. Imagine creating a large program. The said program crashes when you use it. It then becomes really important on finding and fixing the problem. Python lets you know where the error occur and what caused the error. This simplifies the error fixing process and it allows for rapid development with the knowledge that errors don't go unnoticed. This is one of the many situations where error handling comes in handy. The Try and Except StatementsPython allows for errors and exceptions to be handled by the program. To do so, you'll need to use both the >>> try: ... print(spam) ... except: ... print("spam isn't defined!") ... spam isn't defined!
Now, you might have noticed that error messages will identify what kind of error happened on the third line. In the case of the earlier example, a Now, what happens if we want to catch a specific error like >>> try: ... print(spam) ... except NameError: ... print("spam isn't defined!") ... except: ... print("An unknown error has occurred!") ... spam isn't defined!
ExceptionsThe Else StatementThe >>> try: ... spam = 7 ... print("I have %d cans of spam!" % spam) ... except: ... print("Some error has occurred!") ... else: ... print("Everything went smoothly!") ... I have 7 cans of spam! Everything went smoothly! The Finally StatementThe >>> try: ... print("pop") ... except: ... print("An error has occurred!") ... else: ... print("Everything went smoothly!") ... finally: ... print("Finishing up now...") ... pop Everything went smoothly! Finishing up now... >>> try: ... print("soda") ... except: ... print("An error has occurred!") ... finally: ... print("Cleaning up any mess...") ... soda Cleaning up any mess... |
Assignments![]() |
|
- ↑ Python Software Foundation. "Exception hierarchy" (HTML). Built-in Exceptions. Retrieved 2015-05-05.