Skip to main content

Timeline for answer to Correct way of handling exceptions in Python? by Ralph

Current License: CC BY-SA 2.5

Post Revisions

5 events
when toggle format what by license comment
Jun 14, 2009 at 5:19 comment added Ralph I see. In this case it is probably best to add a wrapper function, which translates the exception (as others have suggested).
Jun 12, 2009 at 13:43 comment added Tom I think you didnt fully get what I was trying to say. Try the following: make yourself an SMTP object, and then try smtp.login() without connecting, and then smtp.sendmail() without connecting, you will see they throw 100% identical exceptions, that you cant distinguish, neither by msg nor by errno
Jun 12, 2009 at 10:07 comment added Ralph The exception should be able to provide its details. E.g. you can use "except gaierror, (code, message):", instead of simple "except gaierror:". Then you have the error code and error message and can use them for detailed error handling, e.g. if code == 11001: print "unknown host name:", message
Jun 12, 2009 at 8:25 comment added Tom i disagree, because as i stated quite a few times above, the error messages would be ambiguous, as 2 different function calls, for example login and sendmail could throw the same exception. If you want to print to the user, or to your log: "login() failed because of xyz" or "sendmail() failed because of xyz" this isnt possible, because both calls may result in the same exception. I want detailed handling what went wrong for logging purposes.
Jun 12, 2009 at 1:39 history answered Ralph CC BY-SA 2.5