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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 9 04:05:16 EDT 2008


On Sat, 08 Mar 2008 22:24:36 -0800, Kay Schluehr wrote:

> On 9 Mrz., 06:30, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
> 
>> 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.
> 
> Exceptions in Python don't necessarily signal errors. Just think about
> StopIteration.

I know that. That's why I said that exceptions were used for signaling 
exceptional events.


> Note also that the common practice of letting *possible* errors passed
> silently is to return None instead of raising an exception. 

"Common"? Who does that?

I know re.search() etc. return None in the event the regex doesn't match. 
That's not an error.



> Moreove people create boilerplate like this
> 
> try:
>    k = lst.index(elem)
>    ...
> except IndexError:
>    pass
> 
> instead of
> 
> with lst.index(elem) as k:
>     ...

Possibly because the with keyword is quite new and many people don't know 
it, and much code was written before it even existed, or they have to 
support Python 2.4 or older.


> It would be interesting to think about SoftException semantics for such
> clauses: lst.index would neither raises a HardException nor does it
> return None but leads to skipping the with-block.
> 
> Is it really so exotic that it requires the demand for more use cases?


Are the existing solutions really so incomplete that we need yet another 
solution?

What problem are you trying to solve with SoftExceptions?

How would changing lst.index() to raise a soft exception help here?

pos = lst.index('foo')
lst[pos] = 'bar'

What is that code going to do if 'foo' isn't found in lst and it raises a 
silent, unhandled SoftException? Do you expect Python to be context 
sensitive, and raise HardExceptions in some places and SoftExceptions in 
others? Who controls that?



-- 
Steven



More information about the Python-list mailing list