0

I'm using Python 3

I've been looking around for an answer to this, but I haven't found it yet. Basically, I'm running several Python scripts into a game engine, and each script has its own entry point.

I'd rather not add try: except blocks through all of my code, so I was wondering if it's at all possible to tell Python to quit (or perhaps assign a custom function to that "callback") on finding its first error, regardless of where or what it found?

Currently, the game engine will continue after finding and hitting an error, making it more difficult than necessary to diagnose issues since running into one error may make a subsequent script not work (as it relies on variables that the error-ing script set, for example). Any ideas?

I know that I could redirect the console to a file to allow for easier scrolling, but just capturing the first error and stopping the game prematurely would be really useful.

Okay, a couple of extra bits of info - sorry for neglecting to say this. The engine I'm using (the Blender Game Engine) is coded in C, so changing the source is more than I'd like to do.


After Googling, it would appear that a similar question with a solid answer has been asked here, which is how to get the last raised exception. If I check the sys module for the presence of the last_value variable and it exists, then I can quit prematurely, as the console would have already printed out the error.

Thanks for the help.

Community
  • 1
  • 1
SolarLune
  • 1,229
  • 3
  • 12
  • 14

1 Answers1

0

You could use atexit.register() to register a function that will be called when your program terminates. Upon termination you would then have to use the current traceback object to print debugging information about the exception that occurred (if any).

Note: this assumes that exceptions AREN'T trapped by the game code, since it relies on the exception to terminate execution.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Hmm, this sounds interesting, but doesn't seem to enable me to stop the program on the first exception. What I'm essentially trying to do is grab the last raised exception regardless of where in the code it was raised from. – SolarLune Jul 10 '14 at 14:58
  • It will give you access to the first exception that threatens to terminate the program. If the exceptions are already being caught by your program logic there isn't much you can do to intercept that. – holdenweb Jul 10 '14 at 15:55