Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Last active August 29, 2015 14:05
Show Gist options
  • Save jorge-lavin/12874985bb23b18d1e6f to your computer and use it in GitHub Desktop.
Save jorge-lavin/12874985bb23b18d1e6f to your computer and use it in GitHub Desktop.
# Code taken from http://codereview.stackexchange.com/a/61539/52090
import sys
exit_codes = [
(ValueError, 10),
(KeyError, 25)
]
# now you can also use a dictionary here, but
# the ordering is not defined, so if you have a
# superclass and subclass, then your code will sometimes
# if the superclass is matched before subclass!
try:
raise Some_Exception
except tuple(e[0] for e in exit_codes) as exit_err:
for exc_class, code in exit_codes:
if isinstance(exit_err, exc_class):
sys.exit(code)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment