how to throw an error in python code example

Example 1: 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 2: throwing an exception python

raise Exception("message")

Example 3: exception pyton print

except Exception as e: print(e)

Example 4: throw error python

raise Exception("Error")

Example 5: 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",)

Example 6: python exception

try:
  # code block
except ValueError as ve:
  print(ve)

Tags:

Cpp Example