Catching and Throwing Exceptions in Python

SIDL exceptions are caught and thrown very much like normal Python exceptions are caught and thrown. Here is an example of a code catching exceptions from a call to getFib.


  try:
    fib.getFib(-1, 10, 10, 0)
  except ExceptionTest.NegativeValueException.Exception:
    (etype, eobj, etb) = sys.exc_info()
    # eobj is the SIDL exception object
    print eobj.getNote()  # show the exception comment
    print eobj.getTrace() # and traceback

Here is an example of a Python implementation function that throws an exception. The setNote method provides a useful error message, and the add method helps provide a multi-language traceback capability (provided each layer of the call stack calls add).


  def getFib(self, n, max_depth, max_value, depth):
    # sidl EXPECTED INCOMING TYPES
    # ============================
    # int n, max_depth, max_value, depth
    #
    # sidl EXPECTED RETURN VALUE(s)
    # =============================
    # int _return
    # DO-NOT-DELETE splicer.begin(getFib)
    if (n < 0):
      ex = ExceptionTest.NegativeValueException.NegativeValueException()
      ex.setNote("n negative")
      ex.add(__name__, 0, "ExceptionTest.Fib.getFib")
      raise ExceptionTest.NegativeValueException.Exception, ex
    # numerous lines deleted
    # DO-NOT-DELETE splicer.end(getFib)





babel-0.10.2
users_guide Last Modified 2005-03-23

http://www.llnl.gov/CASC/components
components@llnl.gov