raise an exception python 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: how to raise a error in python

# You can raise a error in python by using the raise keyword
raise Exception("A error occured!")

Example 3: throwing an exception python

raise Exception("message")

Example 4: raise exception in python

raise Exception('I know Python!') # Don't! If you catch, likely to hide bugs.

Example 5: raise python

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

Example 6: except as Exception:

>>> def catch():
...     try:
...         asd()
...     except Exception as e:
...         print e.message, e.args
... 
>>> catch()
global name 'asd' is not defined ("global name 'asd' is not defined",)

Tags:

Misc Example