C ++:main()未捕获的异常的自定义格式 [英] C++: Custom formatting for exceptions uncaught by main()

查看:68
本文介绍了C ++:main()未捕获的异常的自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a library that contains functions that can throw exceptions. To debug programs that use my library, I would like to provide a custom format-method that will give the programmer more information about these exceptions if they are uncaught by main().

通常,可以从最终用户编写的main function()调用我的库.最终用户不会在main()中放置try..catch块,因为最终用户不会期望这些异常(实际上应该避免在我的库和main()之间使用其他有问题的库来阻止和/或捕获它们,但是它们不是,那是我们需要调试的东西."

Generally, my library can be called from a main function() written by an end user. The end user does not put a try..catch block in main() because the end user does not expect these exceptions (they should actually be avoided and/or caught by other, buggy libraries, between my library and main(), but they're not, and that's what we need to debug).

// 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();
}

运行上面的程序,我得到:

When I run the above program, I get:

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

我喜欢一条错误消息告诉我未捕获的异常的方式.我想知道在My_Exception上添加钩子的最佳方法,以便在这种情况下也可以打印msg字符串.

I like the way there is an error message telling me about the uncaught exception. I would like to know the best way to add a hook to My_Exception so that the msg string would also be printed in this situation.

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

I am willing to register callbacks with the runtime system, or add methods to My_Exception, but I don't want to mess with main() itself. (I know this problem could be solved by telling the linker to use a different entry point having a try..catch, and wrapping main() in that, but it will be hard to get the end-user to adopt something like that).

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

Clearly there is already some exception-checking code after main(), as the above message was printed. The stack trace is:

#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表示程序在Library_Function中中止.听起来很不对劲;在main()无法捕获异常之后,它至少应该已经从main()退出.必须有一些语言细节,例如它可以保留堆栈,直到处理异常为止?无论如何,我离题了.

Aside: I don't at all understand why gdb says the program is aborting in Library_Function. That sounds wrong; it should at least have exited from main() after main() failed to catch the exception. Must be some language detail, like it preserves the stack until the exception is handled? In any case, I digress.

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

Maybe we can extend std::terminate() or cxa__throw() or some other runtime component to print msg in this case?

这个问题有何不同

为什么我不能从我的throw异常中打印出错误? 2个答案-相似,但是1.我的问题涉及一个异常对象(不是字符串),因此有关自定义格式(问题标题中)的要点是相关的. 2.标题缺少关键字未捕获",很难找到

How come I don't can't print out error from my throw exception? 2 answers -- similar, but 1. my question involves an exception object (not a string) and therefore the point about custom formatting (in the question title) is relevant. 2. missing keyword "uncaught" from the title, so hard to find

未通过what()1答案打印的重新抛出异常的自定义错误消息 -1.在他们的问题中已经包含了我的问题的答案,因此不能是同一问题.除非您认为用什么工具砸钉子"与为什么我的锤子不起作用"是同样的问题. 2.标题中缺少关键字未捕获"

Custom error message of re-thrown exception not printed by what() 1 answer -- 1. already contains an answer to my question in their question, so cannot be the same question. Unless you consider "what tool pounds a nail" to be the same question as "why isn't my hammer working". 2. missing keyword "uncaught" from the title

针对虚拟常量字符 ro_err :: StdErr :: what()const的更宽松的抛出说明符1个答案* -1.在他们的问题中已经包含了我的问题的答案,因此不能是同一问题.除非您认为用什么工具砸钉子"与为什么我的锤子不起作用"是同样的问题. 2.标题中缺少关键字未捕获"

looser throw specifier for ‘virtual const char ro_err::StdErr::what() const’ 1 answer* -- 1. already contains an answer to my question in their question, so cannot be the same question. Unless you consider "what tool pounds a nail" to be the same question as "why isn't my hammer working". 2. missing keyword "uncaught" from the title

推荐答案

如πάνταῥεῖ所建议,您可以尝试

As suggested by πάντα ῥεῖ, you can try this

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

这篇关于C ++:main()未捕获的异常的自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆