exceptions considered harmful

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Jun 18 05:45:23 EDT 2005


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

> "H. S. Lahman" <h.lahman at verizon.net> 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.





More information about the Python-list mailing list