Difference between revisions of "Exception Handling in Python"

From PeformIQ Upgrade
Jump to navigation Jump to search
(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 ...')
(No difference)

Revision as of 22:38, 19 September 2011


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