python捕获异常堆栈跟踪与未捕获异常堆栈跟踪

时间:2019-07-09 06:12:11

标签: python-2.7

我在我的代码中尝试了一次except代码块,希望能得到django.core.exceptions.ValidationError类型的异常,捕获并记录下来。我尝试了各种方法来记录异常,从简单的logger.excepption到追溯库的所有功能。以下是我每次都得到的堆栈跟踪:

Traceback (most recent call last):
  File "/home/belong/work/code/hulk/hulk/commons/decorators.py", line 48, in func_wrapper
    func(*args, **kwargs)
  File "/home/belong/work/code/hulk/hulk/job/views.py", line 211, in create
    raise DjangoCoreValidationError('test')
ValidationError: [u'test']

如果我根本没有捕获到异常,python将其记录为未捕获的异常,并且我得到以下堆栈跟踪:

2019-07-09 06:09:56,445 ERROR MainProcess [hulk.commons.exceptions.handlers: 35] [oid: 706a0f00ea9d476291ba118e61576622] [strail: hulk] [aid: ] Uncaught exception
Traceback (most recent call last):
  File "/home/belong/work/code/hulk/venv/local/lib/python2.7/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/belong/work/code/hulk/hulk/job/views.py", line 211, in create
    raise DjangoCoreValidationError('test')
ValidationError: [u'test']

这两个为何不同?有什么办法可以在捕获和记录相同的异常时获得与未捕获的相同的堆栈跟踪?

0 个答案:

没有答案