Get exception type in python 1.5.2

How can I get exception type in python 1.5.2?

:

try:
    raise "ABC"
except Exception as e:
    print str(e)

gives a SyntaxError:

  except Exception as e:
                    ^

SyntaxError: invalid syntax

EDIT: this does not work:

try:
    a = 3
    b = not_existent_variable
except Exception, e:
    print "The error is: " + str(e) + "\n"

a = 3
b = not_existent_variable

as I only get the argument and not the actual error (NameError):

The error is: not_existent_variable

Traceback (innermost last):
  File "C:\Users\jruegg\Desktop\test.py", line 8, in ?
    b = not_existent_variable
NameError: not_existent_variable
+3
source share
1 answer

it

except Exception, e:

In Python 1 and 2. (Although it asalso works in Python 2.6 and 2.7).

(Why are you using 1.5.2 !?)

Then enter the type of error you are using type(e). To get the type name in Python 2, you use type(e).__name__, I have no idea if this works in 1.5.2, you will need to check the documents.

Update: it is not, but e.__class__.__name__did.

+9
source

Source: https://habr.com/ru/post/1780629/


All Articles