且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向用户显示异常调试信息

更新时间:2023-12-04 09:34:34

包装所有的代码在一个 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;
    }
}