Exception Handling in Python
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 ...')
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