# File: exceptions-example-1.py # python imports this module by itself, so the following # line isn't really needed # import exceptions class HTTPError(Exception): # indicates an HTTP protocol error def __init__(self, url, errcode, errmsg): self.url = url self.errcode = errcode self.errmsg = errmsg def __str__(self): return ( "" % (self.url, self.errcode, self.errmsg) ) try: raise HTTPError("http://www.python.org/foo", 200, "Not Found") except HTTPError, error: print "url", "=>", error.url print "errcode", "=>", error.errcode print "errmsg", "=>", error.errmsg raise # reraise exception ## $ python exceptions-example-1.py ## url => http://www.python.org/foo ## errcode => 200 ## errmsg => Not Found ## Traceback (innermost last): ## File "exceptions-example-1", line 16, in ? ## HTTPError: