C ++中的异常的调用栈 [英] Call-stack for exceptions in C++

查看:327
本文介绍了C ++中的异常的调用栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,在我的C ++多平台代码中,我对每个函数都有一个try-catch。在每个catch块中,我将当前函数的名称添加到异常并再次抛出,以便在最后的catch块(我最终打印异常的细节),我有完整的调用堆栈,这有助于我跟踪异常的原因。

Today, in my C++ multi-platform code, I have a try-catch around every function. In every catch block I add the current function's name to the exception and throw it again, so that in the upmost catch block (where I finally print the exception's details) I have the complete call stack, which helps me to trace the exception's cause.

这是一个好的做法,还是有更好的方法来获取异常的调用堆栈?

Is it a good practice, or are there better ways to get the call stack for the exception?

推荐答案

不,它是非常可怕的,我不明白为什么你需要一个调用堆栈在异常本身 - 我找到异常原因,行号和代码的文件名初始例外发生相当充分。

No, it is deeply horrible, and I don't see why you need a call stack in the exception itself - I find the exception reason, the line number and the filename of the code where the initial exception occurred quite sufficient.

说到这里,如果你真的必须有一个堆栈跟踪,要做的事情是在异常throw站点生成调用堆栈信息ONCE。没有单一的可移植的方式这样做,但使用像 http://stacktrace.sourceforge.net/ 结合和类似的VC ++的库不应该太难。

Having said that, if you really must have a stack trace, the thing to do is to generate the call stack info ONCE at the exception throw site. There is no single portable way of doing this, but using something like http://stacktrace.sourceforge.net/ combined with and a similar library for VC++ should not be too difficult.

这篇关于C ++中的异常的调用栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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