Last active
July 19, 2019 16:49
-
-
Save miklevin/be79424def3936496c924dc6f11f9a69 to your computer and use it in GitHub Desktop.
Problems figuring out what error name to use in except? Try this!
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
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