exception problem

Charles Hixson charleshixsn at earthlink.net
Sun Jun 24 19:16:25 EDT 2012


On 06/24/2012 03:43 PM, MRAB wrote:
> On 24/06/2012 23:26, Charles Hixson wrote:
>> The code:
>>                   print    ("pre-chunkLine")
>>                   chunks    =    []
>>                   try:
>>                       chunks    =    self.chunkLine (l)
>>                   except:
>>                       print    ("caught exception")
>>                       print (sys.exc_info()[:2])
>>                   finally:
>>                       print ("at finally")
>>                   print ("chunks =")
>>                   print (repr(chunks), ".", end = ":")
>> produces this result:
>>     . . ., by
>> pre-chunkLine
>> caught exception
>> at finally
>> path  3...
>>
>> Any suggestions as to what's wrong with the code?
>> FWIW, chunkLine begins:
>>       def chunkLine (self, line):
>>           print    ("chunkLine: ")
>>           print ("line = ", line)
>>           if    line == None:
>>               return    []
>>           assert    (isinstance (line, str) )
>>
> Don't use a bare "except"; it'll catch _any__exception. Catch only what
> you expect.
>
> For all I know, it could be that the name "l" doesn't exist.
But what I wanted was to catch any exception.  A problem was happening 
and I had no clue as to what it was.  (It turned out to be "self is not 
defined".  A silly mistake, but a real one.)

The odd thing was that if I ran it without the try block, I didn't get 
any exceptions at all.  (Which I clearly should have, except that since 
self wasn't defined, I'd usually expect the interpreter to detect the 
error before trying to execute the code.)

-- 
Charles Hixson




More information about the Python-list mailing list