Displaying exception debug information to users

后端 未结 4 1601
轮回少年
轮回少年 2020-12-13 16:40

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 e

4条回答
  •  悲&欢浪女
    2020-12-13 17:28

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

提交回复
热议问题