Django:捕获所有错误并显示错误和消息的类型

use*_*003 11 django exception-handling

我正在尝试创建一个通用的错误处理程序,类似于Oracle中的"其他人".我可以找到的示例都涉及捕获特定的预期错误.

Try:
    some_function()
Except: #I don't know what error I'm getting
    show_me_error(type_of_error_and_message)
Run Code Online (Sandbox Code Playgroud)

DrT*_*rsa 33

try:
    1/0
except Exception as e:
    print '%s (%s)' % (e.message, type(e))

>>> 
integer division or modulo by zero (<type 'exceptions.ZeroDivisionError'>)
Run Code Online (Sandbox Code Playgroud)

这是非常有据可查的.

但也许你只需要Sentry

  • Python 3语法:print('%s(%s)'%(e.message,type(e))) (4认同)

小智 10

import traceback

try:
    some_function()
except Exception as e:
    trace_back = traceback.format_exc()
    message = str(e)+ " " + str(trace_back)
    print message
Run Code Online (Sandbox Code Playgroud)