Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • Exactly! That's the problem. When I put the master inside the try..except, I am pointed to the exception line of the master function, and then I would have no idea what happened, and would spend an hour tyring to debug. But then again, if all 10 lines can cause potential problems... what do you do? What I mean is that testings can eliminate a lot of the errors, but for a web application, one function can call 10 different functions to construct a response object. But when it returns, one of the attributes is empty, and the master cannot use this incomplete response object. Commented Jul 13, 2012 at 21:36
  • @CppLearner -- Then it's up to the programmer. If there's a sensible action to take when an incomplete response is recieved, then a try/except makes sense (e.g. try to get the response again, or skip this response, etc.). of course, if you have control over why the response was bad, you may want to at least print a warning before trying again so you know there's a problem ... If there's no way to recover from this error, it's best to not have a try/except so you can track down the reason why you got a bad response in the first place. Commented Jul 13, 2012 at 21:43
  • 1
    thanks for the suggestion. Do you mind to look at the updated code. Am I still doing the wrong thing? Commented Jul 13, 2012 at 22:47
  • 1
    @CppLearner -- There isn't really a point to doing try: something(); except Exception as e: raise e. You're catching the exception and re-raising it. Ultimately, the caller sees the same thing. If you catch an exception, you should do something. you can do something with the exception (e.g. print (e)), or you can just take some other action -- e.g. try to run the command again or print that the command was poorly formed, print the string and exit, ... But if you catch an exception, you should do more than just re-raise it. Commented Jul 13, 2012 at 22:55
  • Right. For addandremove I should do this if applicable: except StandardError: //do something more than just raising. Then again, this becomes really really confusing to me. If there is a 3rd function that calls addandremove() in one of the statements, do we also surround the call with a try..except block? What if this 3rd function has 3 lines, and each function calls itself has a try-except? I am sorry for building this up. But this is the sort of problem I don't get. Commented Jul 13, 2012 at 23:13