Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24406

Re: exception problem

Date 2012-06-24 16:16 -0700
From Charles Hixson <charleshixsn@earthlink.net>
Subject Re: exception problem
References <4FE79433.8020704@earthlink.net> <4FE7982C.5020708@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.1466.1340579780.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: exception problem Charles Hixson <charleshixsn@earthlink.net> - 2012-06-24 16:16 -0700
  Re: exception problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-06-25 07:48 +0000
    Re: exception problem Charles Hixson <charleshixsn@earthlink.net> - 2012-06-27 17:13 -0700
      Re: exception problem alex23 <wuwei23@gmail.com> - 2012-06-27 17:36 -0700
      Re: exception problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-06-28 03:44 +0000
    Re: exception problem Ethan Furman <ethan@stoneleaf.us> - 2012-06-28 05:52 -0700

csiph-web