Re: exceptions considered harmful

Discussion in 'Python' started by Roy Smith, Jun 18, 2005.

  1. Roy Smith

    Roy Smith Guest

    "H. S. Lahman" <> wrote:
    > > Never throw an exception. And if someone throws one at you,
    > > catch it immediately and don't pass it on.

    >
    > IMO, this is generally fine advice. Languages provide exception
    > handlers so that applications have a chance to respond gracefully when
    > the software is in an unstable state. IOW, you should never see an
    > exception unless the software is seriously broken. A corollary is that
    > if the software is corrupted, then even processing the exception becomes
    > high risk. So one should do as little as possible when processing
    > exceptions. (Some languages provide a degree of bullet proofing, but
    > that just make the exception handling facility too expensive to use for
    > routine processing.)


    This sounds like a very C++ view of the world. In Python, for example,
    exceptions are much more light weight and perfectly routine.
    Roy Smith, Jun 18, 2005
    #1
    1. Advertising

  2. On Fri, 17 Jun 2005 20:00:39 -0400, Roy Smith wrote:

    > "H. S. Lahman" <> wrote:
    >> > Never throw an exception. And if someone throws one at you,
    >> > catch it immediately and don't pass it on.

    >>
    >> IMO, this is generally fine advice. Languages provide exception
    >> handlers so that applications have a chance to respond gracefully when
    >> the software is in an unstable state. IOW, you should never see an
    >> exception unless the software is seriously broken. A corollary is that
    >> if the software is corrupted, then even processing the exception becomes
    >> high risk. So one should do as little as possible when processing
    >> exceptions. (Some languages provide a degree of bullet proofing, but
    >> that just make the exception handling facility too expensive to use for
    >> routine processing.)

    >
    > This sounds like a very C++ view of the world. In Python, for example,
    > exceptions are much more light weight and perfectly routine.



    Yes. Furthermore, there is a reason why they are called exceptions and not
    errors. Exceptions don't necessarily mean "something has gone wrong". They
    can also mean, "something has gone right, but it is an exceptional case".

    For example, Guido recommends using exceptions for handling exceptional
    cases for command-line tools. Eg, something like this:

    try:
    for switch in sys.argv[1:]:
    if switch in ("-h", "--help"):
    raise HelpException
    else:
    do_something()
    except HelpException:
    print __doc__
    sys.exit(0) # because asking for help is not an error
    except:
    print "Program failed!"
    sys.exit(1) # but an error is an error
    do_main_calculation()
    sys.exit(0)

    Or something like this:

    try:
    # long slow calculation
    do_lots_of_work_here()
    if condition():
    # we can jump out of the slow calculation and take a short cut
    raise QuickException
    # and more long slow calculation here
    partial_result = do_lots_more_work_here()
    except QuickException:
    # short and easy calculation
    partial_result = do_quick_calculation()
    # in either case, we need to do more work here
    return do_more_work(partial_result)

    Or even roll-back of processing:

    try:
    process_data() # can raise RecoverableError or FatalError
    except RecoverableError:
    roll_back()
    except FatalError:
    print "A fatal error occurred."
    sys.exit(1)

    Because Python's handling of try...except is lightweight and fast, this is
    an idiom which is very practical to use in many situations where it
    wouldn't be practical in other languages.

    In conclusion: in Python, it is just not true that "you should never see
    an exception unless the software is seriously broken". It is very bad
    advice to say that "even processing the exception becomes high risk".

    If the exception genuinely is an error, then it may be difficult to
    recover, and the best thing you can do is fail gracefully. But that's no
    reason to not use exceptions.



    --
    Steven.
    Steven D'Aprano, Jun 18, 2005
    #2
    1. Advertising

  3. On Fri, 17 Jun 2005 20:00:39 -0400, Roy Smith <> wrote:

    >This sounds like a very C++ view of the world. In Python, for example,
    >exceptions are much more light weight and perfectly routine.


    The problem with exceptions is coping with partial
    updatd state. Suppose you call a complex computation
    routine (say a boolean operation between winged edge
    data structures representing nurbs boundary of two
    solids) and that you get back a "ZeroDivision"
    exception... how good is the data structure now ?

    Either you have some way to be able to easily *guarantee*
    coherence or you're doomed. Allowing the user to continue
    without being sure about what is in memory is not going
    to be that helpful; the result could be that instead
    of losing last ten minuts you're going to waste the
    last month of user work (because the user will save
    the corrupted data and will notice problems only much
    later). If however you can restore the situation by a
    rollback or by loading a preimage, or you know that
    the operation works only *reading* two solids and
    creating a new one *and* you know that it's not a problem
    to drop a broken solid data structure then I think it's
    ok to swallow an exception.

    IMO either you know exactly what caused the exception
    and how the state got influenced, or you must have thick
    logical walls protecting you and allowing the problem
    to not propagate (for example an RDBMS rollback facility).

    With python it's sort of easy to get rollback for class
    instances if the code to protect doesn't plays strange
    tricks. Even C++ is powerful enough to allow that.

    With C++ care must be taken to just avoid memory leaks
    or other resource related problem while in python this
    is rarely a problem; but the real issue with exception is
    partial state update and in this python is not different.
    If you really have to check every place for possible
    exceptions then the exception machinery is not such
    a big win compared to return codes. IMO exceptions
    are nice when there are many raise and few try or
    except in the code; but then either you have state
    protection or swallowing an exception is taboo.

    Andrea
    Andrea Griffini, Jun 18, 2005
    #3
    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. Robert Mischke
    Replies:
    3
    Views:
    1,520
    Tony Morris
    May 19, 2005
  2. Toby A Inkster
    Replies:
    19
    Views:
    739
  3. Andy Dingley

    XTech conference considered harmful?

    Andy Dingley, Feb 13, 2006, in forum: HTML
    Replies:
    2
    Views:
    588
    Richard Sexton
    Feb 14, 2006
  4. Replies:
    25
    Views:
    1,301
    Isaac To
    Oct 31, 2003
  5. Replies:
    9
    Views:
    530
Loading...

Share This Page