C++ 抛出“Poco::SystemException”实例后调用终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1762535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
terminate called after throwing an instance of 'Poco::SystemException'
提问by Omry Yadan
Sometimes (about 1 out of 100 runs), my program terminates with this message:
有时(大约 100 次运行中的 1 次),我的程序会以以下消息终止:
terminate called after throwing an instance of 'Poco::SystemException'
what(): System exception
my code is not the one catching the exception (all my catches are more verbose), and I am not sure where it's caught. it's very likely that the exception does contain a useful message, but it's not returned through the what() method but by the displayText() method.
我的代码不是捕获异常的代码(我所有的捕获都更加冗长),而且我不确定它是在哪里捕获的。异常很可能确实包含有用的消息,但它不是通过 what() 方法返回,而是通过 displayText() 方法返回。
The string "terminate called after throwing an instance of" has ~600k in Google, so it's probably printed by code inserted by the compiler or by some common library (pthread?). I only seen this error message when the program ran on Linux (never on Windows).
字符串“抛出实例后调用的终止”在谷歌中有~600k,所以它可能是由编译器或某些公共库(pthread?)插入的代码打印的。当程序在 Linux 上运行时(从未在 Windows 上运行),我只看到此错误消息。
anyone knows in what code this uncaught exception is caught?
有谁知道在什么代码中捕获了这个未捕获的异常?
回答by Ferdinand Beyer
anyone knows in what code this uncaught exception is caught?
有谁知道在什么代码中捕获了这个未捕获的异常?
An uncaught exception is—by definition—not caught anywhere.
根据定义,未捕获的异常在任何地方都未捕获。
If an exception cannot be handled, the C++ exception mechanism will call std::terminate()
(see include header <exception>
), which will call a customizable termination handler. On your platform, the standard termination handler prints the output of std::exception::what()
(which Poco's exceptions inherit from). Unfortunately, the way Poco's exceptions are designed, this will not contain any useful information.
如果无法处理异常,则 C++ 异常机制将调用std::terminate()
(请参阅包含标头<exception>
),这将调用可自定义的终止处理程序。在您的平台上,标准终止处理程序打印输出std::exception::what()
(Poco 的异常继承自)。不幸的是,Poco 异常的设计方式,这将不包含任何有用的信息。
There are multiple ways an exception cannot be handled:
有多种方法无法处理异常:
- No suitable
catch()
handler is found and the unwinding mechanism exitsmain()
. You can try wrapping yourmain()
code intry...catch
to print the exception'sdisplayText()
. - A function exits with an exception that does not match its exception specification (
... functionname(...) throw(...)
). This will callstd::unexpected()
which in turn will callstd::terminate()
(by default). - An exception is thrown from within a destructor that is called during the unwinding process of another exception. Never throw exceptions in destructors!
- An exception is thrown while trying to create the original exception object. Never throw exceptions in custom exception classes!
- 找不到合适的
catch()
处理程序,展开机制退出main()
。您可以尝试将main()
代码包装起来try...catch
以打印异常的displayText()
. - 函数退出时出现与其异常规范 (
... functionname(...) throw(...)
)不匹配的异常。这将调用std::unexpected()
,而后者将调用std::terminate()
(默认情况下)。 - 在另一个异常的展开过程中调用的析构函数中抛出异常。永远不要在析构函数中抛出异常!
- 尝试创建原始异常对象时抛出异常。永远不要在自定义异常类中抛出异常!
When using Poco threads and a thread is terminated by an unhandled exception, Poco will invoke its internal ErrorHandler
and the program will not exit, so I doubt that this is a threading issue.
当使用 Poco 线程并且线程被未处理的异常终止时,Poco 将调用其内部ErrorHandler
并且程序不会退出,因此我怀疑这是一个线程问题。
回答by Gaurav Raj
I was getting the same error. I had used a try catch block in the run function of the Poco::Runnable class. I removed the try catch block from this class and used a derivative of Poco::ErrorHandler class to handle the errors. After this I stopped getting this error.
我遇到了同样的错误。我在 Poco::Runnable 类的 run 函数中使用了 try catch 块。我从这个类中删除了 try catch 块,并使用了 Poco::ErrorHandler 类的派生类来处理错误。在此之后,我不再收到此错误。