繁体   English   中英

C++:main() 未捕获的异常的自定义格式

[英]C++: Custom formatting for exceptions uncaught by main()

我正在创建一个包含可以抛出异常的函数的库。 为了调试使用我的库的程序,我想提供一个自定义格式方法,如果它们未被main()捕获,它将为程序员提供有关这些异常的更多信息。

通常,我的库可以从最终用户编写的main function() 调用。 最终用户不会在main()放置try..catch块,因为最终用户不希望这些异常(它们实际上应该被其他有缺陷的库避免和/或捕获,在我的库和main() ,但是他们不是,这就是我们需要调试的)。

// 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字符串。

我愿意向运行时系统注册回调,或向My_Exception添加方法,但我不想弄乱main()本身。 (我知道这个问题可以通过告诉链接器使用具有try..catch的不同入口点并在其中包装main()来解决,但很难让最终用户采用类似的方法) .

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

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

Library_Function :我完全不明白为什么gdb说程序在Library_Function中止。 这听起来不对; 它至少应该在main()未能捕获异常之后从main()退出。 必须是一些语言细节,比如它会保留堆栈直到处理异常? 无论如何,我离题了。

在这种情况下,也许我们可以扩展std::terminate()cxa__throw()或其他一些运行时组件来打印msg

这个问题有什么不同

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

重新抛出异常的自定义错误消息未由 what() 打印 1 个答案-- 1. 已经在他们的问题中包含了我的问题的答案,因此不能是同一个问题。 除非您认为“什么工具可以敲钉子”与“为什么我的锤子不工作”是同一个问题。 2. 标题中缺少关键字“uncaught”

'virtual const char ro_err::StdErr::what() const' 1 个回答* -- 1. 已经在他们的问题中包含了我的问题的答案,所以不能是同一个问题。 除非您认为“什么工具可以敲钉子”与“为什么我的锤子不工作”是同一个问题。 2. 标题中缺少关键字“uncaught”

正如 πάντα ῥεῖ 所建议的,你可以试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM