Q:

Python get an error code from exception

In python, I have code that handles exceptions and prints error codes and messages.
try:
    somecode() #raises NameError
except Exception as e:
    print('Error! Code: {c}, Message, {m}'.format(c = e.code, m = str(e))

However, e.code is not the proper way to get the error name (NameError), and I cannot find the answer to this. How am I supossed to get the error code?

A:

Try this:
try:
    somecode() #raises NameError
except Exception as e:
    print('Error! Code: {c}, Message, {m}'.format(c = type(e).__name__, m = str(e)))

Read this for a more detailed explanation.

