显示异常的调试信息给用户

显示异常的调试信息给用户

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

问题描述

目前,我正在努力增加异常和异常处理我的申请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,当未处理的异常时,用户将获得一个很好的堆栈跟踪和一般大量无价的调试信息。如果你在一个开放源码软件应用程序的工作,让用户粘贴到的信息报告,问题是...好让我们只说我发现很难活不下去了。对于这个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 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.

当然,我会处理code时,我可以在里面异常,但我没那么天真的以为我不会让经历了两个滑(无意的,当然)。

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).

所以,我想要做的是包装一个尝试 catch块创建里面我主要切入点一个特殊的对话框,告知已发生的应用程序错误的用户,更详细的信息,presented当用户点击更多或调试信息或什么。这将包含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?

推荐答案

结束语所有code在一个的try / catch 块都OK。它不会放慢它里面的东西执行,例如。事实上,我所有的程序都(code类似),这个框架:

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;
    }
}

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

07-30 04:28