Chapter 12 - Exceptions
Exercise 1: Rescuing Exceptions
The call to trip_over_curb
raises an exception. Instead of allowing execution to halt, rescue the exception, and print the string "Caught you!"
.
Here’s the program output right now:
When you’re ready, have a look at the solution.
Exercise 2: Handling the exception object
The call to send_sos
raises an exception with a message containing some coordinates. Rescue the exception, and print “Sending rescue party to” followed by the coordinates in the exception message.
Here’s the output:
When you’re ready, have a look at the solution.
Exercise 3: Handling multiple exception types
The call to random_failure
raises either a ThisError
, ThatError
, or TheOtherError
, randomly. Rescue ThisError
, and print the message “I caught this!”. Rescue ThatError
, and print “I caught that!”. Allow TheOtherError
to fail normally.
When you run your code repeatedly, you should see one of these three outputs:
When you’re ready, have a look at the solution.