1 | initial version |
The name of a caught exception in Python can be obtained using the as
keyword, which assigns the exception instance to a variable. For example:
try:
# Some code that might raise an exception
except Exception as e:
print(type(e).__name__)
In this example, the caught exception instance is assigned to the variable e
. The type(e)
function returns the type of the exception (e.g. ValueError
, TypeError
, etc.), and the __name__
attribute returns its name as a string.