Instead of encapsulating my entire code in a try{} except{} block, is there someway of catching exceptions globally?
Basically I am looking for a way to have a global exception handler which will handle all unhandled exceptions in开发者_JS百科 the my python application written for google app engine
If you're using the webapp framework, you should already be defining a subclass of RequestHandler that serves as a base class, with all your app's handlers extending that. You can simply override handle_exception, which serves as a global exception handler for any uncaught exceptions.
The default implementation calls self.error(500), logs the exception, and if debug is on, outputs a stacktrace.
If you're using another framework, you could write a piece of WSGI middleware that calls the wrapped WSGI app, and catches any thrown exceptions, dealing with them as you wish.
Well, at the most basic level you could wrap all of your handler scripts referenced by app.yaml in a giant try-except block.
If you are using the webapp framework, consider overriding handle_exception()
for each of your request handlers. If you want all of your request handlers to have some basic exception handling that you specify, you could a request handler which implements this method and then derive all of your handlers from it.
You application probably has a main()
function, put the try/except in that function, and it'll catch everything from your application.
精彩评论