What c.l.py's opinions about Soft Exception?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 9 06:30:11 CET 2008


On Sat, 08 Mar 2008 19:51:24 -0800, Lie wrote:

> Soft Exception
> What is "Soft Exception"?
> Soft Exception is an exception that if is unhandled, pass silently as if
> nothing happened. For example, if a variable turns into NoneType, it'll
> raise Soft Exception that it have become NoneException, programmers that
> wants to handle it can handle it with a try...except block while
> programmers that doesn't care about it (or know it won't be a problem to
> his code) can just leave the code as it is.
> 
> Soft Exception differs from Hard Exceptions (the regular Exception) in a
> way that Hard Exception must be handled at all cost or the program will
> be terminated while Soft Exception allow programmers not to handle it if
> they don't want to.

I don't think that there are very many cases where exceptions can be 
ignored safely. There are two main reasons for using exceptions:

(1) Signaling an exceptional event.

(2) An error occurred.

I can't think of many cases where you would wish to ignore either, and 
just continue processing. The only examples I can think of are in loops, 
where you are doing the same thing over and over again with just a little 
change, and you wish to skip any problematic data, e.g.:

def plot_graph(func, domain):
    for x in domain:
        plot(x, func(x))

If an error occurs in plot() for one particular x value, you would want 
to ignore it and go on to the next point. But that's easy enough to do 
with a regular try...except block.

Simply put, you're suggesting the following two alternatives:

Hard Exceptions: terminate the program unless explicitly silenced
Soft Exceptions: pass silently unless explicitly caught

In this case, I agree with the Zen of Python ("import this"):

Errors should never pass silently.
Unless explicitly silenced.

The cost of explicitly silencing exceptions is tiny, the risk of misuse 
of Soft Exceptions is very high, and the benefit of them is negligible.


-- 
Steven



More information about the Python-list mailing list