exception problem

Chris Angelico rosuav at gmail.com
Sun Jun 24 19:51:15 EDT 2012


On Mon, Jun 25, 2012 at 9:16 AM, Charles Hixson
<charleshixsn at earthlink.net> wrote:
> 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.)

Python, not having any concept of declared variables, can't detect
such errors prior to execution. The error isn't like in C where you're
trying to use a variable that's not declared; the error is that, at
run time, there's no global with the name "self". That's why it's an
exception, not a compile-time error.

But the real question is: Why do you get no exception traceback if you
remove the try/except? Is something else swallowing everything thrown?
This is something that you will need to solve. (And it's a pretty
annoying issue. We had the same thing here at work, though in
Javascript not Python; a framework was swallowing exceptions, so all
sorts of work was being done blindfolded. Legacy code is not fun. Life
got a lot easier for us last Friday when I found and excised the
offending try/catch.) Hunt around and see if exceptions are getting
logged someplace other than your console - not uncommon if, for
instance, you're running in a web server. This is not going to be the
only time when you get an exception that could be massively helpful.

Mind you, I think every programmer should spend some time debugging
blind. It gives you such an appreciation for interactive debuggers.
Plus, it's an exercise in making your problems reproducible, if you
have to start your program over every time you add some more
information to it :)

ChrisA



More information about the Python-list mailing list