On Nov 13, 7:11 pm, "Emanuele D'Arrigo" <[EMAIL PROTECTED]> 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?
That depends on the situation, doesn't it? For example, if I want to solve a linear congruence X*a == Z (mod Y) all I have to do is check that GCD(X,Y) divides Z and I can go ahead and call the solving function... ...which requires use of the modular inverse function which raises an exception if GCD(X,Y) is not 1 (even if it does divide Z). But wait! If, in fact, the GCD(X,Y)>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 -- http://mail.python.org/mailman/listinfo/python-list