Unit 9: Exception Handling
Any programmer should be able to identify the source of potential errors and implement code to handle those errors. This unit introduces the syntax necessary for achieving this goal. Handling errors can be a sensitive topic because the programmer must address points where something could go wrong. Devote yourself to these examples, as they will be important in your journey to becoming a professional programmer.
Completing this unit should take you approximately 4 hours.
Upon successful completion of this unit, you will be able to:
- explain how exceptions are implemented capture programming errors; and
- write programs that handle errors by using try, except, and finally statements.
9.1: Catching and Handling Errors
When writing software, as you may have experienced already, errors can occur. As we gain experience, we will learn to anticipate what kinds of errors can occur during program execution. For example, when performing a division calculation num/den between two variables "num" and "den", it is possible for the variable den to contain a value of zero. If this happens, a "division by zero" error should be generated to avoid a catastrophic calculation. Exceptions are a way of catching and handling potential errors so that a program will not crash and stop executing in an abrupt, untimely fashion.
The video provided in this subsection is essential for new programmers because the types of errors that can occur must be identified, discussed, and understood. The discussion on handling errors will be used as a springboard for understanding the rest of this unit.
The "try" and "except" commands are the gateway for handling errors. "try" allows you to try to execute your code, and "except" identifies the type of error to be handled. Python contains many built-in exceptions that can be raised using the "except" command. For example,
- ValueError: raised if an operation or function receives an argument that has an inappropriate value
- NameError: raised if a variable name that does not exist is referenced
This example shows how to handle a division by zero error by raising the built-in ZeroDivisionError exception. Make sure to practice executing this code in Repl.it.
Well-structured code attempts to identify everything that can go wrong as well as everything that can go right. The "else" and "finally" commands, while optional, are important components of professionally written code. The "else" command can be used as part of a "try-except" block to execute code if no errors in the set of errors being tested have occurred. The "finally" section of code will always run, but it helps to programmatically delineate such code from a software organization perspective to distinguish it from code that is being checked for errors.
Now that the motivation and some syntax for exceptions has been presented, we can delve a bit deeper into the structure of writing programs containing exceptions. The following lesson is designed to help put into context the rudiments just presented. In addition, one more important component of exception handling is the 'raise' statement which is useful for raising an exception if, for example, it occurs inside an exception handler.
This set of examples reviews the try, except, and raise commands. Make sure to practice them. All too often in introductory programming courses, the practice of handling errors, if taught at all, is brought up as an afterthought or some ancillary programming feature. Nothing can be further from the truth. To become a professional programmer, testing, debugging, and creating bulletproof software lies at the heart of software design. This is why alpha and beta versions exist. You can set yourself apart by refining your expertise in exception handling.
9.2: Handling a File Error
As already pointed out in the exceptions lesson, Python contains a vast set of built-in exceptions. One important class has to do with file handling. The example presented in the lesson will help to review file handling methods and put them in the context of handling exceptions.
Study Session Video Review
Unit 9 Review and Assessment
In this video, course designer Eric Sakk walks through the major topics we covered in Unit 9. As you watch, work through the exercises to try them out yourself.
- Receive a grade
Take this assessment to see how well you understood this unit.
- This assessment does not count towards your grade. It is just for practice!
- You will see the correct answers when you submit your answers. Use this to help you study for the final exam!
- You can take this assessment as many times as you want, whenever you want.