Last active
April 15, 2016 05:18
-
-
Save belisarius222/24d45f2bd0682a763d3eca118ab8ef3d to your computer and use it in GitHub Desktop.
Custom Python Exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HandGrenadeError: pass | |
class WrongNumberError(HandGrenadeError): pass | |
class HolyHandGrenade: | |
def reference(self): | |
return "Armaments, chapter two, verses nine through twenty-one." | |
def lob(self, number): | |
if number != 3: | |
raise WrongNumberError("Wrong number: {}".format(number)) | |
print("Lobbest thou thy Holy Hand Grenade of Antioch towards thy foe,") | |
print("who, being naughty in my sight, shall snuff it.") | |
def countdown(): | |
grenade = HolyHandGrenade() | |
print("Consulting the book of armaments:") | |
print(grenade.reference()) | |
for count in [1, 2, 5, 3]: | |
try: | |
grenade.lob(count) | |
except WrongNumberError as e: | |
print("{} is right out!".format(count)) | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment