代码之家  ›  专栏  ›  技术社区  ›  personal_cloud

C++:对主(未)的异常进行自定义格式化

  •  1
  • personal_cloud  · 技术社区  · 6 年前

    我正在创建一个库,其中包含可以抛出异常的函数。为了调试使用我的库的程序,我想提供一个定制的格式方法,如果这些异常没有被发现的话,这个方法可以给程序员更多关于这些异常的信息 main()

    main 函数()由 . 最终用户没有将 try..catch 阻止 主() 因为最终用户并不期望这些异常(它们实际上应该被我的库和 主()

    // The following example would actually be multiple files,
    // but to keep this example simple, put it in "<filename>"
    // and compile the following with "g++ <filename>".
    
    
    // library file
    
    class My_Exception
    {
    public:
      char const* msg;
      My_Exception(char const* msg) : msg(msg) {}
    };
    
    void Library_Function(bool rarely_true = false)
    {
      if (rarely_true)
        throw My_Exception("some exceptional thing");
    }
    // note to discerning user: if you use the "rarely_true" feature,
    // be sure to remember to catch "My_Exception"!!!!
    
    
    // intermediate, buggy, library (written by someone else)
    
    void Meta_Function()
    {
      Library_Function(true); // hahahaha not my problem!
    }
    
    
    // main program (written by yet someone else, no "try..except"
    // allowed here)
    
    int main()
    {
      Meta_Function();
    }
    

    当我运行上述程序时,我得到:

    terminate called after throwing an instance of 'My_Exception'
    Abort (core dumped)
    

    My_Exception 所以 msg 在这种情况下也会打印字符串。

    ,但我不想惹麻烦 主() ,和包装 但很难让最终用户采用这样的方法)。

    很明显,之后已经有一些异常检查代码了 主() ,因为上面的消息是打印出来的。堆栈跟踪是:

    #0  0x0000155554c0d428 in __GI_raise (sig=sig@entry=6)
        at ../sysdeps/unix/sysv/linux/raise.c:54
    #1  0x0000155554c0f02a in __GI_abort () at abort.c:89
    #2  0x000015555502e8f7 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #3  0x0000155555034a46 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #4  0x0000155555034a81 in std::terminate() ()
       from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #5  0x0000155555034cb4 in __cxa_throw ()
       from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #6  0x00000000004006eb in Library_Function() ()
    #7  0x00000000004006f4 in main ()
    (gdb)
    

    gdb 说这项计划将在2012年中止 Library_Function 主() 主() 未能捕获异常。必须是一些语言细节,比如它在处理异常之前保留堆栈?无论如何,我离题了。

    也许我们可以延长 std::terminate() cxa__throw() 或其他运行时组件打印 味精 在这种情况下?

    为什么我不能从抛出异常中打印出错误?2个答案 --类似,但1.我的问题涉及一个异常对象(不是字符串),因此关于自定义格式(在问题标题中)的观点是相关的。2.标题中缺少关键字“uncaught”,很难找到

    what()1应答未打印重新抛出异常的自定义错误消息

    虚常量字符的松散抛出说明符 --1.在他们的问题中已经包含了我的问题的答案,所以不能是同一个问题。除非你认为“什么工具能钉钉子”和“为什么我的锤子不能用”是同一个问题。2.标题中缺少关键字“uncaught”

    1 回复  |  直到 6 年前
        1
  •  1
  •   seccpur    6 年前

    根据建议± áá, 你可以试试这个

    class myexception : public exception
    {
    public:    
        const char* what() const noexcept override
        {
            char* ch = "some exceptional thing";
            return ch;
        }
    };    
    
    void Library_Function(bool rarely_true = false)
    {
        if (rarely_true)
            throw myexception();
    }
    
    int main()
    {
        try 
        {
            Library_Function(true);
        }
        catch (myexception& err)
        {
            std::cout << err.what();
        }
        return 0;
    }