Skip to content

Instantly share code, notes, and snippets.

@miklevin
Last active July 19, 2019 16:49
Show Gist options
  • Save miklevin/be79424def3936496c924dc6f11f9a69 to your computer and use it in GitHub Desktop.
Save miklevin/be79424def3936496c924dc6f11f9a69 to your computer and use it in GitHub Desktop.
Problems figuring out what error name to use in except? Try this!
from sys import exc_info as error
trapped = lambda x: print("Trapped: %s" % x)
err = lambda : print(error()[0].__name__)
try :
#1/0
#1 + 'foo'
#int('foo')
foo
except ZeroDivisionError:
trapped(1)
except TypeError:
trapped(2)
except ValueError:
trapped(3)
except NameError:
trapped(4)
except:
# Prints name of error to trap
err()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment