The Samurai Principle

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Sep 8 17:13:25 CEST 2010


On Wed, 08 Sep 2010 14:44:12 +0000, Grant Edwards wrote:

> On 2010-09-08, Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au>
> wrote:
>> On Tue, 07 Sep 2010 20:35:45 -0700, Phlip wrote:
>>
>>> Exceptions are very dangerous by themselves, because if you don't trap
>>> them just right they can cause side-effects.
>>
>> Huh?
>>
>> If you don't trap them just right, they cause a stack trace,
> 
> Not always.  That is the effect of not trapping them at all. However,
> you can trap them incorrectly -- which can result in hard-to-track down
> problems.
> 
> The main example of this is a "bare except" clause that not only catches
> and handles the "expected" exception but also catches (and
> mishandles/ignores) an unexpected one.

Ah, fair enough. That would be a false positive error -- catching too 
much rather than too little.

Still, that's no better, or worse, than misinterpreting special error 
codes that are returned by functions. The main error mode there is 
catching too little -- people neglect to check the error code, and 
therefore have buggy code:

p = some_string.find('#')
print some_string[:p]

And who hasn't done this more than once?

re.search(pattern, some_string).group()



-- 
Steven



More information about the Python-list mailing list