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

查看:172
本文介绍了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.

话虽如此,如果你真的需要一个堆栈跟踪,那么要做的就是在异常抛出站点生成调用堆栈信息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天全站免登陆