Exception Handling in Python

From PeformIQ Upgrade
Revision as of 23:38, 19 September 2011 by PeterHarding (talk | contribs) (Created page with ' =Custom Exceptions= Code similar to that seen above can be used to create custom exceptions and pass information along with them. This can be extremely useful when trying to ...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Custom Exceptions

Code similar to that seen above can be used to create custom exceptions and pass information along with them. This can be extremely useful when trying to debug complicated projects. Here is how that code would look; first creating the custom exception class:

  class CustomException(Exception):
      def __init__(self, value):
          self.parameter = value
      def __str__(self):
          return repr(self.parameter)

And then using that exception:

try:
    raise CustomException("My Useful Error Message")
except CustomException, (instance):
    print "Caught: " + instance.parameter