CS105 Study Guide

Unit 9: Exception Handling

9a. Explain how exceptions are implemented capture programming errors

  • What are exceptions?
  • Why is it necessary to catch programming errors?
  • Explain how the try and except statements are used.

Ideally, after a program is written, it should be completely debugged, meaning that it should not generate errors. In reality, for practical applications with unpredictable user input, this is not the case. When programming, you should be continually addressing factors that could lead to a program crash. If not, then program users will experience program crashes. Python provides a consistent structure known as exception handling and the error that occurs is referred to as an exception. For instance,

a='asdfasdf'
sum(a)

generates a TypeError exception because the sum function is expecting numerical data. Python is designed to catch exceptions. However, if no exception handler exists, the exception will result in the termination of program execution; obviously, an unwanted state of affairs.

The most basic type of exception handler involves two statements: try and except. The indented clause following the try statement is the code that is intended to execute. If an error occurs while attempting to execute the try clause, an indented except clause will execute in response to an exception (assuming the appropriate exception has been identified). Python is equipped with powerful tools for exception handling, but the basic flow is similar to if elif conditional statements.

To review, see Example: Try and Except and Example: ZeroDivisionError.

 

9b. Write programs that handle errors by using try, except, and finally statements

  • Explain how the else and finally statements are used.
  • What are some typical built-in Python exceptions one should know?
  • How are multiple exceptions handled?

In addition to try and except, an else clause can be included in the event that an exception does not occur. Finally (no pun intended), the finally statement can be used to house all code intended to execute whether or not an exception has occurred. Your expertise with if else elif statements should directly translate into writing try except else finally exception handlers.

A blanket except statement without referring to any specific exception will catch any error; however, this scenario makes it difficult to write a useful exception handler as the specific error has not been identified. The Python language has made provision for several built-in exceptions that you should know:

  • IndexError: illegal index into an ordered contained such as a list or a tuple
  • KeyError: illegal key into a dictionary
  • TypeError: a function gets argument of improper type
  • ValueError: a function gets argument of correct type but improper value
  • ZeroDivisionError: division by zero
  • NameError: a variable is not found in local or global scope

In addition, multiple exceptions using multiple except clauses are handled in the order they appear in the exception handler.

To review, see Example: Else and Finally and Exceptions Lesson.

 

Unit 9 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • debugged 
  • program crash
  • exception handling
  • exception
  • try statement 
  • except clause 
  • else clause
  • finally statement 
  • IndexError
  • KeyError
  • TypeError
  • ValueError
  • ZeroDivisionError
  • NameError