Timeline for answer to Correct way of handling exceptions in Python? by dbr
Current License: CC BY-SA 2.5
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 15, 2009 at 15:51 | comment | added | dbr | No problem! "but generally repackaging is considered bad practise" I don't consider my second code repackaging, it's just taking advantage of classes (which in Python are just ways of grouping together a bunch of functions/variables) - I'm not suggesting you redistribute that class alone, just treat it like any another function | |
| Jun 15, 2009 at 10:13 | comment | added | Tom | I chose this as the accepted answer because I think my nested exception handling is not very readable although I thought it would be the proposed way using the else-statement of try...except block. For your second example: It's a nice way of handling this problem, but generally repackaging is considered bad practise. Thanks for your valuable input dbr :) | |
| Jun 15, 2009 at 10:11 | vote | accept | Tom | ||
| Jun 12, 2009 at 21:38 | comment | added | dbr |
Generally I don't think you should nestle try/except blocks, especially not 3 levels, or with as much code as in the question - maybe for sequentially trying to import a module (like try to import cElementTree, then ElementTree, finally try lxml.et.ElementTree for example)
|
|
| Jun 12, 2009 at 14:45 | history | edited | dbr | CC BY-SA 2.5 |
Typos in first code
|
| Jun 12, 2009 at 8:30 | comment | added | Tom | then now it boils down to a question of principle. your first code is basically the same as mine, except that you simply dont nest the exceptions, as I did in an "else"-tree, which was suggested in the python docs. which one is better practise? the docs state that else should always be preferred rather than additional statements in the try block. its basically the same question with if's: is it better to return before another if, or is it better to nest the ifs conditionally. | |
| Jun 12, 2009 at 3:07 | comment | added | mhawke | In your first example, send_message() will always return after the server.login() and never send the message. I don't think that there should be a finally for this statement. | |
| Jun 12, 2009 at 2:20 | comment | added | Tom Leys | +1 I really like how you have solved the "library only uses one exception for everything" problem | |
| Jun 12, 2009 at 1:53 | history | answered | dbr | CC BY-SA 2.5 |