In Class Activity (Not a Deliverable):

Objective: Learn how to use special objects called exceptions to manage errors that arise during a program’s execution.

Description: First, make a copy of Exercise 9. Then, at the end of the Python file do the following:

  • Key Error Instructions:
    • Cause your file to raise an exception by trying to access the property (or key) “emailAddress” for the current user in the try block. In the exception handler (exception block) print a notification stating that an error occurred because the email address was not defined in the dictionary.
  • Type Error instructions:
    • Cause your file to raise a second exception by trying to access the user’s name and adding a fictitious 920 number (to produce a hypthetical verification code) for the current user in the try block. In the exception handler (exception block) print a notification stating that an error occurred because the two data types can’t be added together.

Solution Example:

1User: Bernard
2    Role: Administrator
3    Chose the following secuirty question: What was the name of your first dog?
4    Answered to the security question: Scully.
5    Error finding emailAddress
6    These two data types can't be added together!
7
8User: Charlotte
9    Role: Guest
10    Chose the following secuirty question: What is your favorite color?
11    Answered to the security question: Purple.
12    Error finding emailAddress
13    These two data types can't be added together!
14
15User: Teddy
16    Role: Guest
17    Chose the following secuirty question: In which city were you born?
18    Answered to the security question: Cullowhee.
19    Error finding emailAddress
20    These two data types can't be added together!