Unusual Exception Behaviour

mk mrkafk at gmail.com
Thu Jul 17 13:10:47 EDT 2008


> http://linux.byexamples.com/archives/365/python-convey-the-exception-traceba
> ck-into-log-file/
>> if __name__=="__main__":
>>      try:
>>          main()
>>      except:
>>          print "Trigger Exception, traceback info forward to log file."
>>          traceback.print_exc(file=open("errlog.txt","a"))
>>          sys.exit(1)
>>
> 
> I've just given this solution a shot but I still seem to get the same
> result, it suddenly starts dumping the log data to the command line, and
> nothing gets printed in error.txt apart from the keyboard interrupt from
> when I kill the application.

That's seriously weird. What's your Python version and platform? On my 
Windows and Linux machines, with more recent Python versions the above 
trick works flawlessly.

Check your environment, namely PYTHON* variables. There may be something 
causing this behaviour. Unset them.

Check the first line of your scripts. If you're calling wrong Python 
interpreter (there may be more than one in the system for some reason), 
this may cause it.

You could also try setting up PYTHONINSPECT environment variable or run 
the python interpreter with -i option before program filename, which 
drops you into an interactive shell upon exception or termination of a 
program.

> For some reason the exceptions don't seem to be raised properly so obviously
> aren't being caught at this higher level.
> 
> So confusing.

This behavior is seriously unusual for Python. Maybe you have some old / 
buggy version?







More information about the Python-list mailing list