Created
March 23, 2021 19:54
-
-
Save alcides/8db5cd22d8f35c50fec288a162b59d31 to your computer and use it in GitHub Desktop.
Rebuilds Exceptions from name
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
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