To throw or to throw not?

Discussion in 'Python' started by Emanuele D'Arrigo, Nov 14, 2008.

  1. 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
     
    Emanuele D'Arrigo, Nov 14, 2008
    #1
    1. Advertising

  2. Emanuele D'Arrigo

    Chris Rebert Guest

    On Thu, Nov 13, 2008 at 5:11 PM, 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?


    Depends on how serious the error is (e.g. str.find() returns -1 rather
    than raising an exception if it can't find the substring), but 98% of
    the time, you'll want to raise an exception; it's Pythonic, idiomatic,
    and expected. You'd have to have a *really* good reason to use an
    error value/code instead.
    Python is not C, and despite what Joel has said On Software, error
    codes generally suck.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...
    http://rebertia.com

    >
    > Manu
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Chris Rebert, Nov 14, 2008
    #2
    1. Advertising

  3. Emanuele D'Arrigo

    Mensanator Guest

    On Nov 13, 7:11 pm, "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?


    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
     
    Mensanator, Nov 14, 2008
    #3
  4. Emanuele D'Arrigo

    Steve Holden Guest

    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/
     
    Steve Holden, Nov 14, 2008
    #4
  5. 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
     
    Steven D'Aprano, Nov 14, 2008
    #5
  6. Emanuele D'Arrigo

    Aaron Brady Guest

    On Nov 13, 10:09 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > 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


    If you need error flags, define them in a class:

    class ErrFlags:
    onlyInCorner= object( )
    livesOnCeiling= object( )

    def myFunc3( ):
    something( )
    return ErrFlags.onlyInCorner

    def myFunc2( ):
    result= myFunc3( )
    if result is ErrFlags.onlyInCorder:
    something
    elif result is ErrFlags.livesOnCeiling:
    something

    In some cases, you might want to call a method on the return object.

    I agree with the fellows earlier. Without further information,
    exceptions are generally nice and solid.
     
    Aaron Brady, Nov 14, 2008
    #6
  7. Thank you all for the insightful replies! Much appreciated!

    Manu
     
    Emanuele D'Arrigo, Nov 15, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,124
    Kevin Spencer
    Oct 27, 2003
  2. Jon Maz
    Replies:
    7
    Views:
    4,351
    Jon Maz
    Oct 25, 2004
  3. moo moo
    Replies:
    3
    Views:
    1,088
    John C. Bollinger
    Oct 22, 2003
  4. Replies:
    15
    Views:
    7,775
    Roedy Green
    Sep 8, 2005
  5. inquirydog
    Replies:
    21
    Views:
    1,875
    inquirydog
    Sep 11, 2004
Loading...

Share This Page