Abuse of the object-nature of functions?

Carl J. Van Arsdall cvanarsdall at mvista.com
Tue Jul 11 14:18:18 EDT 2006


Bruno Desthuilliers wrote:
> Carl J. Van Arsdall wrote:
>   
>> Sybren Stuvel wrote:
>>
>>     
>>> Ant enlightened us with:
>>>  
>>>
>>>       
>>>>     try:
>>>>         assertion = callable.is_assertion
>>>>     except:
>>>>         pass
>>>>     
>>>>         
>>> Try to make a habit out of catching only the exceptions you know will
>>> be thrown. Catching everything generally is a bad idea. In this case,
>>> my bet is that catching AttributeError is enough.
>>>
>>>   
>>>       
>> What about doing exception kind of like a C switch statement with a
>> default case:
>>
>> try:
>>  do_something()
>> except TypeError:
>>  fix_something()
>> except:
>>  print "Unknown error, you are doomed"
>>  traceback.print_exc()  #something to print the traceback
>>  exit_gracefully()
>>
>> Is this frowned upon?  You still handle the error and you know where it
>> happened and what happened.  Anything wrong with this?  I don't like the
>> idea of my system crashing for any reason.
>>     
>
> It may be a good idea to do something like this *at the top level of the
> application*. But take time to carefully read the standard exceptions
> hierarchy in the fine manual - you'll notice some exception you perhaps
> don't want to catch or at least don't want to display (hint: look for
> the warnings hierarchy and for SysExit...)
>
>   
Hrmms, well, here's an interesting situation.  So say we wanna catch 
most exceptions but we don't necessarily know what they are going to 
be.  For example, I have a framework that executes modules (python 
functions), the framework wraps each function execution in a try/except 
block in order to compensate for what *might* happen.  Upon coding the 
framework I really have no idea what types of problems these modules 
might have but I want to catch these errors so that I can clean up and 
exit gracefully, not only that but I want to dump the exception to log 
files so that we can attempt to fix it.  So, I have the option of 
catching all standard exceptions and not list the ones I know I don't 
want to catch.  But what about user defined exceptions?  Do I then have 
to enforce policies on the system stating what types of exceptions can 
be raised? 

Is there a way in python to say, "hey, catch everything but these two"? 



-- 

Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software




More information about the Python-list mailing list