在Python中如何以编程方式检查异常的堆栈跟踪? [英] How can you programmatically inspect the stack trace of an exception in Python?

查看:157
本文介绍了在Python中如何以编程方式检查异常的堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Python发生异常时,可以检查堆栈吗?你能确定它的深度吗?我查看了追溯模块,但我不知道如何使用

When an exception occurs in Python, can you inspect the stack? Can you determine its depth? I've looked at the traceback module, but I can't figure out how to use it.

我的目标是捕获在解析eval表达式期间发生的任何异常,而不会捕获它可能调用的任何函数抛出的异常。不要谴责我使用eval。这不是我的决定。

My goal is to catch any exceptions that occur during the parsing of an eval expression, without catching exceptions thrown by any functions it may have called. Don't berate me for using eval. It wasn't my decision.

注意:我想以编程方式,而不是交互式地执行。

NOTE: I want to do this programmatically, not interactively.

推荐答案

您可以使用检查模块,其具有用于跟踪的一些效用函数。查看框架对象的属性概述。

You can use the inspect module which has some utility functions for tracing. Have a look at the overview of properties of the frame objects.

这篇关于在Python中如何以编程方式检查异常的堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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