EAFP vs LBYL (was Re: A little disappointed so far)

Carl Banks imbosol at aerojockey.com
Mon May 19 16:04:18 EDT 2003


Gerhard H?ring wrote:
> Carl Banks wrote:
>> Alex Martelli wrote:
>> 
>>>Bare except is definitely NOT what I'm advocating, of course.  I don't
>>>think I've ever caught anything broader than StandardError in production
>>>code, actually (except in weird cases to accomodate 'host code' that
>>>might be raising strings, was specifically untrusted, and the like) --
>>>and when I do catch so widely is typically one of the few cases where
>>>I'll use isinstance, in order to reraise at once if I've happened to
>>>catch a KeyboardInterrupt (why the [expletive deleted] KeyboardInterrupt
>>>couldn't have been taken away from the part of the subtree derived from
>>>StandardError, I definitely dunno).
>>  
>> Out of curiosity, why not do this?
>> 
>>     try:
>>         ...
>>     except KeyboardInterrupt:
>>         handle_interrupt()
>>     except:
>>         raise
> 
> Isn't the "except: raise" implicit if you omit it?

Yes.  Silly me.  I really meant:

    try:
        ...
    except KeyboardInterrupt:
        raise
    except:
        handle_exception()


-- 
CARL BANKS




More information about the Python-list mailing list