5.2 Exception Handling
109
Python has many different exception types, and we use two of them here,
KeyboardInterrupt and ValueError (some more examples will be given soon).
Unless the user answers with a valid integer, one of these exceptions is raised. A
KeyboardInterrupt is raised if we type Ctrl-c to stop execution, whereas a
ValueError is raised otherwise. We note that in each case an appropriate printout is
given first. Furthermore, when a ValueError is raised, execution proceeds directly
with the next question (after the printout). When a KeyboardInterrupt is raised,
the printout is succeeded by execution of the break statement. This implies that
execution breaks out of the for loop and the program stops after printing the final
score.
One dialogue with the program could then be, for example:
*** Welcome to the times tables test! ***
(To stop: ctrl-c)
6 * 8 = 48
Correct!
Your score is now: 1
5 * 8 = u
(accidentally hit wrong key - author’s comment)
You must give a valid number!
3 * 10 =
(only press enter - author’s comment)
You must give a valid number!
5 * 6 = 30
Correct!
Your score is now: 2
7 * 6 =
(type ctrl-c - author’s comment)
Ok, you want to stop!
Finished!
Your final score: 2
(max: 100)
With our final version, we see that some typical error situations are handled according to plan, and also that Ctrl-c now works as previously. For the present problem,
we found that only two different types of exceptions (KeyboardInterrupt and
ValueError) were required. Had more exceptions been needed, we could just have
extended the structure straight forwardly, with
except exception_type:
for each of them. Note that it is possible to have a unified response to several
exceptions, by just collecting the exception types in a parentheses and separating
them with a comma. For example, with two such exceptions, they would appear on
the form
except (exception_type_1, exception_type_2):
Before ending this chapter on exception handling, it is appropriate to briefly
exemplify a few more of the many built-in exceptions in Python.
109
Python has many different exception types, and we use two of them here,
KeyboardInterrupt and ValueError (some more examples will be given soon).
Unless the user answers with a valid integer, one of these exceptions is raised. A
KeyboardInterrupt is raised if we type Ctrl-c to stop execution, whereas a
ValueError is raised otherwise. We note that in each case an appropriate printout is
given first. Furthermore, when a ValueError is raised, execution proceeds directly
with the next question (after the printout). When a KeyboardInterrupt is raised,
the printout is succeeded by execution of the break statement. This implies that
execution breaks out of the for loop and the program stops after printing the final
score.
One dialogue with the program could then be, for example:
*** Welcome to the times tables test! ***
(To stop: ctrl-c)
6 * 8 = 48
Correct!
Your score is now: 1
5 * 8 = u
(accidentally hit wrong key - author’s comment)
You must give a valid number!
3 * 10 =
(only press enter - author’s comment)
You must give a valid number!
5 * 6 = 30
Correct!
Your score is now: 2
7 * 6 =
(type ctrl-c - author’s comment)
Ok, you want to stop!
Finished!
Your final score: 2
(max: 100)
With our final version, we see that some typical error situations are handled according to plan, and also that Ctrl-c now works as previously. For the present problem,
we found that only two different types of exceptions (KeyboardInterrupt and
ValueError) were required. Had more exceptions been needed, we could just have
extended the structure straight forwardly, with
except exception_type:
for each of them. Note that it is possible to have a unified response to several
exceptions, by just collecting the exception types in a parentheses and separating
them with a comma. For example, with two such exceptions, they would appear on
the form
except (exception_type_1, exception_type_2):
Before ending this chapter on exception handling, it is appropriate to briefly
exemplify a few more of the many built-in exceptions in Python.
