Skip to content

Instantly share code, notes, and snippets.

@alcides
Created March 23, 2021 19:54
Show Gist options
  • Save alcides/8db5cd22d8f35c50fec288a162b59d31 to your computer and use it in GitHub Desktop.
Save alcides/8db5cd22d8f35c50fec288a162b59d31 to your computer and use it in GitHub Desktop.
Rebuilds Exceptions from name
import importlib
class MyError(Exception):
pass
def __str__(self):
return "HERE"
try:
print("hi")
raise MyError("ola")
except Exception as e:
module = e.__class__.__module__
name = e.__class__.__name__
k = importlib.import_module(module)
r = getattr(k, name)
# This is an alternative that does not consider modules
# r = globals()[name]()
print(r, type(r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment