向用户显示异常调试信息 [英] Displaying exception debug information to users

查看:143
本文介绍了向用户显示异常调试信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的OSS应用程序添加异常和异常处理。例外从一开始就是一般的想法,但是我想找到一个很好的异常框架,并且在所有的诚实之中,在开始使用它们之前,了解C ++异常处理惯例和成语。我有很多C#/ .Net,Python和其他使用异常语言的经验。我对这个想法并不陌生(但远离主人)。

I'm currently working on adding exceptions and exception handling to my OSS application. Exceptions have been the general idea from the start, but I wanted to find a good exception framework and in all honesty, understand C++ exception handling conventions and idioms a bit better before starting to use them. I have a lot of experience with C#/.Net, Python and other languages that use exceptions. I'm no stranger to the idea (but far from a master).

在C#和Python中,当发生未处理的异常时,用户获得了一个很好的堆栈跟踪,一般来说,很多非常有用的无价值调试信息。如果您正在开发OSS应用程序,将用户将该信息粘贴到问题报告中就可以了...好吧,我想说如果没有这样的话,我很难生活。对于这个C ++项目,我得到应用程序崩溃,或者来自更多知情的用户,我做了X,Y和Z,然后它崩溃了。但是我也想要调试信息!

In C# and Python, when an unhandled exception occurs, the user gets a nice stack trace and in general a lot of very useful priceless debugging information. If you're working on an OSS application, having users paste that info into issue reports is... well let's just say I'm finding it difficult to live without that. For this C++ project, I get "The application crashed", or from more informed users, "I did X, Y and Z, and then it crashed". But I want that debugging information too!

我已经(而且很难)使我的和平与事实,我永远不会看到一个跨平台和交叉编译方式获取C ++异常堆栈跟踪,但我知道我可以获取函数名称和其他相关信息。

I've already (and with great difficulty) made my peace with the fact that I'll never see a cross-platform and cross-compiler way of getting a C++ exception stack trace, but I know I can get the function name and other relevant information.

现在我想要我的未处理的异常。我正在使用 boost :: exception ,他们有非常好的 diagnostic_information thingamajig可以打印出(未调整的)函数名称,文件,行,最重要的是程序员添加到该异常中的其他异常特定信息。

And now I want that for my unhandled exceptions. I'm using boost::exception, and they have this very nice diagnostic_information thingamajig that can print out the (unmangled) function name, file, line and most importantly, other exception specific information the programmer added to that exception.

自然,我会处理代码中的异常,只要我可以,但我不是那么天真,认为我不会让一对夫妇通过(无意中,当然)。

Naturally, I'll be handling exceptions inside the code whenever I can, but I'm not that naive to think I won't let a couple slip through (unintentionally, of course).

所以我想做的是将一个 try 块中的主要入口包裹到一个 catch 中,创建一个特殊的对话框通知用户应用程序发生错误,当用户点击更多或调试信息或其他任何内容时,会显示更详细的信息。这将包含来自diagnostic_information的字符串。然后,我可以指示用户将这些信息粘贴到问题报告中。

So what I want to do is wrap my main entry point inside a try block with a catch that creates a special dialog that informs the user that an error has occurred in the application, with more detailed information presented when the user clicks "More" or "Debug info" or whatever. This would contain the string from diagnostic_information. I could then instruct the users to paste this information into issue reports.

但是,一个呕吐的直觉感觉告诉我,将一切都包装在一个try块中是一个非常糟糕的主意。我要做愚蠢的事情吗?如果是(即使不是),什么是更好的方法来实现我想要的?

But a nagging gut feeling is telling me that wrapping everything in a try block is a really bad idea. Is what I'm about to do stupid? If it is (and even if it's not), what's a better way to achieve what I want?

推荐答案

包装所有的代码在一个 try / catch 块是一个好的。例如,它不会减慢其中任何内容的执行速度。事实上,我的所有程序都有(代码类似)这个框架:

Wrapping all your code in one try/catch block is a-ok. It won't slow down the execution of anything inside it, for example. In fact, all my programs have (code similar to) this framework:

int execute(int pArgc, char *pArgv[])
{
    // do stuff
}

int main(int pArgc, char *pArgv[])
{
    // maybe setup some debug stuff,
    // like splitting cerr to log.txt

    try
    {
        return execute(pArgc, pArgv);
    }
    catch (const std::exception& e)
    {
        std::cerr << "Unhandled exception:\n" << e.what() << std::endl;
        // or other methods of displaying an error

        return EXIT_FAILURE;
    }
    catch (...)
    {
        std::cerr << "Unknown exception!" << std::endl;

        return EXIT_FAILURE;
    }
}

这篇关于向用户显示异常调试信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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