Advanced Python Development Course
Chapter
>
Level
Exception Handling
Custom Exceptions
Objective
Use custom exceptions to collect messages with product orders and send them out via carrier pigeon.
Some messages are deployed across the farm and other stations via messenger pigeon, it would be a good idea to send out a few to stay ahead of schedule. We’ve been catching errors using common exceptions, but sometimes you may benefit from making your own custom exceptions for very specific scenarios. We’re going to source messages and select pigeons by identifying the correct items using our very own custom exceptions.
Custom exceptions are put together by creating classes, by adding the (Exception) clause you can repurpose a class for executing exceptions. For our purposes we will be creating two: InvalidLocation which is activated when a specific string doesn’t pass verification, and UnsuitableSelection which is used to check if a number crosses a certain threshold.
class InvalidLocation(Exception):
"Raised when location is invalid"
pass
class UnsuitableSelection(Exception):
"Raised when pidgeon is 2 or younger"
pass
Now that the classes are set up, it’s time to go acquire the memos so they can be sent out. Create a list named prep in order to store and combine the components needed for the upcoming exchange. Walk to the dark X mark over the red carpet, on the string of tables with X marks in front we’ll be searching for memos that have orders bound for the western sector of the farm.
There are four variable constants used in this segment: red_memo , blue_memo, green_memo and orange_memo . Only memos that are marked to be delivered at "west" sector are to be collected so they can be sent out. To determine if we take the memo or not, we will use try/except and else code blocks in conjunction with the InvalidLocation custom exception. To use the custom exception, have the try check if the memo is labeled "west" and if it’s not to raise the exception. The except block confirms the memo is invalid and the else block collects, and appends to the prep list, the memo if it turns out is bound for the "west" sector. For example:
try: if red_memo != "west": raise InvalidLocation except InvalidLocation: player.speak("This is not an order that needs to be sent out") else: player.collect(red_memo) prep.append(red_memo)
Walk to each of the dark X marks over the colored carpets in front of the tables and run the exception sequence using the proper variable constants corresponding to each color.
Once you’ve collected the memos, walk to the gold X mark in front of the door and use the open() function to access the pigeon coups. Once inside walk to the light X mark, turn around, use the close() function to close the door in order to ensure no pigeons escape.
Walk to the dark X mark over the purple carpet, on the pens we will be verifying if the pigeon is old enough to fly a message or if it’s a hatchling unable to make a delivery. There are four more variable constants to be used in this segment: purple_pen, yellow_pen, white_pen and pink_pen corresponding to each colored carpet. We will be using the same technique as with the memos, using the second class that was set up.
Use the try/except and else code blocks with UnsuitableSelection custom exception to identify if the pigeon is old enough to make the delivery. The try block verifies if the variable constant representing the pen has a suitable pigeon by checking if the bird is younger than two (2) years old. If the pigeon is unsuitable the except block is triggered, otherwise the else block is activated allowing you to append a pigeon to the prep list in order to deliver the message.
try: if purple_pen <= 2: raise UnsuitableSelection except UnsuitableSelection: player.speak("Pigeon unsuitable to deliver") else: prep.append(purple_pen) player.speak("Pigeon Collected")
Go to each dark X mark over the colored carpets and use this sequence with the correct variable constant corresponding to the correct colored carpet. Once you’ve collected the pigeons and messages, walk to the gold X mark at the very bottom and use the place() function with the prep list you created in order to send out the pigeons to deliver messages and complete the level.