简体   繁体   English

osX上的C ++未捕获异常

[英]c++ uncaught exceptions on osX

I have a problem with a c++ library that I develop: I defined my own exception class (that basically only contains a string to return for the what() call) and when an exception is not caught, the goal is that the operating system would print the message from what() and terminate. 我开发的c ++库有一个问题:我定义了自己的异常类(基本上只包含要返回what()调用的字符串),当未捕获到异常时,目标是操作系统将从what()打印消息并终止。 This works well on Linux but on osX, the exception message is not shown (ie it directly terminates without printing anything). 这在Linux上运行良好,但在osX上,未显示异常消息(即,它直接终止而不打印任何内容)。

So, am I correctly assuming that uncaught exceptions would lead to what() followed by a termination? 因此,我是否正确地假设未捕获的异常会导致what()之后终止? What should I do on osX so the message would be shown? 我应该在osX上做什么以便显示该消息? (knowing that I don't like the idea of setting an exception handler, since this imposes the user of the library to do it) (知道我不喜欢设置异常处理程序的想法,因为这强加了库的用户执行此操作)

Thanks! 谢谢! Mathias 马蒂亚斯

On all OS's: catch the exception in main() and print out whatever message is appropriate. 在所有操作系统上:在main()捕获异常并打印出适当的消息。 If you don't catch it, it is unspecified whether destructors are run on the way out. 如果您不了解它,则不确定析构函数是否在出口运行。 Do you really want that? 你真的想要那个吗?

So, am I correctly assuming that uncaught exceptions would lead to what() followed by a termination? 因此,我是否正确地假设未捕获的异常会导致what()之后终止?

No. The standards don't require uncaught exceptions to print out what() . 不需要。标准不需要未捕获的异常即可打印出what() In fact, they make it pretty hard (but not impossible) to write a conforming implementation that always does so. 实际上,它们使编写一个始终如此的一致性实现变得非常困难(但并非不可能)。

There's really no good way to do exactly what you want to do (print out the what() , only if the implementation isn't going to do so in this case, then carry on to terminate() as if the exception had never been caught). 确实没有一种很好的方法来精确地执行您想做的事情(打印出what() ,只有在这种情况下实现不这样做的情况下,然后继续执行terminate()就像从未发生过异常一样抓住)。 But that probably isn't what you really want. 但这可能不是您真正想要的。

This is probably what you want: 这可能是您想要的:

#import <exception>
#import <iostream>

int real_main(int argc, char *argv[]);

int main(int argc, char *argv[]) {
  try {
    return real_main(argc, argv);
  } catch (std::exception &e) {
    std::cerr << "exception: e.what()" << "\n";
    exit(1);
  } catch (...) {
    std::cerr << "exception not derived from std::exception\n";
    exit(1);
  }
}

int real_main(int argc, char *argv[])
{
    // real main code goes here
}

If you really want to, say, _exit or abort (or even terminate ) instead of exit ing, you can do that of course. 如果您真的想_exitabort (甚至terminate )而不是exit ,则可以这样做。 But if you don't know, you probably want exit . 但是,如果您不知道,则可能要exit

Meanwhile: 与此同时:

… knowing that I don't like the idea of setting an exception handler, since this imposes the user of the library to do it …知道我不喜欢设置异常处理程序的想法,因为这迫使图书馆的用户去做

You're writing a library? 你在写图书馆吗? And you want your library to be able to terminate the process behind the application's back? 您是否希望您的库能够终止应用程序背后的进程?

That's probably a very bad idea. 那可能是一个非常糟糕的主意。

But if you really do want to do it, the same code above will work, you just have to wrap it around the entry points to your library instead of around main . 但是,如果您确实想这样做,则可以使用上面相同的代码,只需将其包装在库的入口点周围,而不是在main周围即可。

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

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