446,285 Members | 1,659 Online
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

To throw or to throw not?

 P: n/a I'm pondering on what is a bit of a philosophical dilemma. When should I throw an exception and when should I not? Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3 (). Suppose myFunc3() has detected a problem. What should it do? Throw an exception, forcing myFunc2() to handle it and/or trigger another exception for myFunc1() to deal with? Or should it simply return a meaningful error code, for myFunc2() and myFunc1() to handle as an option but not forcing them to do so? Manu Nov 14 '08 #1
6 Replies

 P: n/a On Thu, Nov 13, 2008 at 5:11 PM, Emanuele D'Arrigo Manu -- http://mail.python.org/mailman/listinfo/python-list Nov 14 '08 #2

 P: n/a On Nov 13, 7:11*pm, "Emanuele D'Arrigo" 1, and I already know that GCD(X,Y) divides Z, then it divides X, Y and Z, so I just divide each by GCD(X,Y) to make a new linear congruence where the modular inverse function will work and I'll get the right answer. The answer is that IF the exception can be handled without the calling function needing to know, then just handle it. Otherwise pass it back in case the calling function can figure out what to do. > Manu Nov 14 '08 #3

 P: n/a Emanuele D'Arrigo wrote: I'm pondering on what is a bit of a philosophical dilemma. When should I throw an exception and when should I not? Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3 (). Suppose myFunc3() has detected a problem. What should it do? Throw an exception, forcing myFunc2() to handle it and/or trigger another exception for myFunc1() to deal with? Or should it simply return a meaningful error code, for myFunc2() and myFunc1() to handle as an option but not forcing them to do so? Remember that with exceptions, if Func2 doesn't want to process the exception it doesn't have to do anything at all to have the exception re-raised: it simply doesn't trap the exception. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Nov 14 '08 #4

 P: n/a On Thu, 13 Nov 2008 17:11:26 -0800, Emanuele D'Arrigo wrote: I'm pondering on what is a bit of a philosophical dilemma. When should I throw an exception and when should I not? Suppose I have myFunc1() calling myFunc2() which in turn calls myFunc3 (). Suppose myFunc3() has detected a problem. What should it do? Throw an exception, forcing myFunc2() to handle it and/or trigger another exception for myFunc1() to deal with? Or should it simply return a meaningful error code, for myFunc2() and myFunc1() to handle as an option but not forcing them to do so? In general, you should raise an exception. Then if myFunc2() can't handle it, it doesn't need to trigger another exception, the exception will just propagate up the call chain to myFunc1(). There are cases where something like a meaningful error value is appropriate, but the only one I can think of right now is NaN in floating point maths. -- Steven Nov 14 '08 #5

 P: n/a On Nov 13, 10:09*pm, Steven D'Aprano

 P: n/a Thank you all for the insightful replies! Much appreciated! Manu Nov 15 '08 #7