Skip to content

Instantly share code, notes, and snippets.

@tawateer
Last active August 29, 2015 14:23
Show Gist options
  • Save tawateer/c07b8cd542c4748ace88 to your computer and use it in GitHub Desktop.
Save tawateer/c07b8cd542c4748ace88 to your computer and use it in GitHub Desktop.
tornado中捕获异常并传给client
#!/bin/env python
import tornado.ioloop
import tornado.web
import tornado.httpserver
def _Exception(func):
def _decorator(*args, **kwargs):
self = args[0]
try:
func(*args, **kwargs)
except Exception, e:
self.write("%s" % e)
return _decorator
class MainHandler(tornado.web.RequestHandler):
@_Exception
def get(self):
raise Exception("Exception: This is a test")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment