klenwell information services : PythonErrorHandling

Python Error Handling

return to DevPython

Basic Usage

try:
    raise Exception("Hello World")
except Exception, e:
    print type(e), e


Custom Exceptions

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


Traceback

try:
    something_that_raises_an_exception()
except Exception, e:
        import traceback; traceback.print_exc()
    #or: import traceback; trace = traceback.format_exc()


References

User-Defined Exceptions (docs.python.org)