python except print error type

Solutions on MaxInterview for python except print error type by the best coders in the world

showing results for - "python except print error type"
Tom
31 Feb 2018
1>>> try:
2...     raise Exception('spam', 'eggs')
3... except Exception as inst:
4...     print(type(inst))    # the exception instance
5...     print(inst.args)     # arguments stored in .args
6...     print(inst)          # __str__ allows args to be printed directly,
7...                          # but may be overridden in exception subclasses
8...     x, y = inst.args     # unpack args
9...     print('x =', x)
10...     print('y =', y)
11...
12<class 'Exception'>
13('spam', 'eggs')
14('spam', 'eggs')
15x = spam
16y = eggs
17