python print exception error code example

Example 1: how to print error in try except python

try:
  # some code
except Exception as e:
	print("ERROR : "+str(e))

Example 2: exception pyton print

except Exception as e: print(e)

Example 3: raise python

# Raise is used to cause an error
raise(Exception("Put whatever you want here!"))
raise(TypeError)

Example 4: python except print error type

>>> try:
...     raise Exception('spam', 'eggs')
... except Exception as inst:
...     print(type(inst))    # the exception instance
...     print(inst.args)     # arguments stored in .args
...     print(inst)          # __str__ allows args to be printed directly,
...                          # but may be overridden in exception subclasses
...     x, y = inst.args     # unpack args
...     print('x =', x)
...     print('y =', y)
...
<class 'Exception'>
('spam', 'eggs')
('spam', 'eggs')
x = spam
y = eggs

Example 5: only try python

try:
    a=2
except:
    pass