Comparison with False - something I don't understand

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Dec 2 12:42:46 EST 2010


Harishankar wrote:
> As I said before, the way exceptions are caught seem to me to be the most 
> confusing bit. Non-atomic operations always worry me. What if my function 
> which is wrapped inside a try block has two different statements that 
> raised the same exception but for different reasons? With error handling 
> I could probably handle it right below the statement which was called and 
> thus reduce the problem???
>
>   

Exception actually are the solution as you can give an unlimitted 
quantity of information:

def myfunction(self):
    error = myfuncException('a meaninful message')
    # error 1
    error.blame = whateverobjectresponsibleoftheerror
    # error 2
    error.blame = anotherobject_anothercause  
   
    raise error

try:
    myfunction()
except myfuncException, exception:
    cause = exception.blame
    # you can inspect the 'cause' for specific handling


In a more general and simple manner, you can tune the error feedback of 
exceptions by changing the message of the exception. Using different 
exception classes is also an obvious way to do it.

But my point was that you can  also set attributes to the exception 
you're raising with reference to almost anything at the time the 
exception occurs. And that is a very cool way to give precise feedback 
to exception handlers.
   
JM



More information about the Python-list mailing list