Skip to content

Instantly share code, notes, and snippets.

@belisarius222
Last active April 15, 2016 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belisarius222/24d45f2bd0682a763d3eca118ab8ef3d to your computer and use it in GitHub Desktop.
Save belisarius222/24d45f2bd0682a763d3eca118ab8ef3d to your computer and use it in GitHub Desktop.
Custom Python Exceptions
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