如何在python中捕获任何异常并打印堆栈跟踪(DJANGO) [英] How to catch any exception in python and print stack trace (DJANGO)

查看:186
本文介绍了如何在python中捕获任何异常并打印堆栈跟踪(DJANGO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样子:

try:
    #bunch of code
except:
    return HttpResponse....

我想向客户端发送错误并打印堆栈跟踪在控制台上。我该怎么办?

I want to send an error to client and to print stack trace on console. How can i do that?

推荐答案

您可以执行以下操作

import traceback 
from django.http import HttpReponse

def view(request):
     try:
          #throw exception
     except:
         tb = traceback.format_exc()
         return HttpResponse(tb)
    # normal flow

这篇关于如何在python中捕获任何异常并打印堆栈跟踪(DJANGO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆