How to raise to throw an exception

Throw an exception

We can use raise statement to throw an exception out of a function.

raise Statement: raise [SomeException [, args [, traceback]]]

Raise error from function


import sys#  w  w  w .j a v a 2 s.com

def excFunc() :
    raise ValueError

try:
    excFunc()
except:
    print "Got an exception"
    print sys.exc_info()

The code above generates the following result.





















Home »
  Python »
    Advanced Features »




Exception Handling
File
Module