traceback messages that are displayed in the various listings. What kind of try except statement
blocks would you write for each one?
Summary
In this hour, you explored how to handle error exceptions with class. By using try except
statement blocks, you learned how to eliminate ugly traceback messages and provide a script user
with clean and user-friendly error messages. You got to play around with try except statement
blocks and some of their options in a script.
In Hour 18, “GUI Programming,” you will take a major step forward in your Python adventure: You
will be learning about GUI programming!
Q&A
Q. I don’t know the exact name of an exception that may be raised in my script. Where can
I get help?
A. If you have a general idea about what kind of error exception may be raised from a Python
statement but don’t have its exact name to use in a try except statement, you can go to
docs.python.org/3/library/exceptions.html for a list of Python exception names and a brief
description of each one. Also, exception groupings are shown at that site.
Q. Can you wrap your mainline function within a try except statement?
A. Yes, but that would not be considered good form. It’s best to keep only the statements that may
raise a particular exception with each try except statement block.
Q. I’ve heard I can raise my own exceptions. Is that true?
A. Yes, it is true. By using the raise Python statement, you can raise exceptions to change the
flow of a script. These exceptions can be built in or custom made. See
docs.python.org/3/tutorial/errors.html for more information on this topic.
Workshop
Quiz
1. Multiple exceptions can be handled within a try except statement block. True or false?
2. A syntax error generates a SyntaxError message. A runtime error raises a(n)
____________ and produces a(n) ___________ message.
3. A(n) _________ statement block is executed when exceptions are raised and when they are not
raised, and a(n) _____________ statement block is executed only when no exceptions are
raised.
a. try; exempt
b. finally; else
c. else; finally
Answers
1. True. Multiple exceptions can be handled within a try except statement block, though it is