Skip to main content

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

Current License: CC BY-SA 2.5

Post Revisions

5 events
when toggle format what by license comment
Jun 12, 2009 at 14:06 comment added David Seiler If each line of your program can throw several different exceptions, and each needs to be handled individually, then the bulk of your code is going to be exception handling. There's nothing bad or unpythonic about that. Just make sure you've picked an approach that scales: if you have ten distinct code groups, each of which can throw 4 different exceptions, then it becomes really important that each code group not introduce a new level of indentation ;). That's what the flattening is for, and that's why I'd be wary of using else:, no matter what the docs say.
Jun 12, 2009 at 2:38 comment added Unknown @Tom +1, that's why I didn't suggest this solution.
Jun 12, 2009 at 1:42 comment added Paul Fisher Watch out on the finally block — you'll want to set server to None before the block in order to avoid accidentally referencing a non-existent variable.
Jun 12, 2009 at 1:19 comment added Tom the point is, as stated above, that they do not throw individual exceptions, so it cant be trivially flattened. In case your connection gets interrupted before you can auth, server.login and server.sendMail could throw the same exception ("connect to server first") But as i also stated above, I am not looking for a solution to this specific problem. I am more interested in a general approach how to solve this. Your second approach is basically my code without "else". its more pretty though i have to admit ;)
Jun 12, 2009 at 1:09 history answered David Seiler CC BY-SA 2.5