main()会捕获线程抛出的异常吗? [英] Will main() catch exceptions thrown from threads?
问题描述
我有一个很大的应用程序,可以动态加载共享对象并在共享对象中执行代码。为了预防起见,我尝试对 main
中的所有内容进行尝试/捕获。我为3件事创建了一个捕获: myException
(内部例外), std :: exception
和 ...
(捕获所有异常)。
I have a pretty large application that dynamically loads shared objects and executes code in the shared object. As a precaution, I put a try/catch around almost everything in main
. I created a catch for 3 things: myException
(an in house exception), std::exception
, and ...
(catch all exceptions).
作为共享对象执行的一部分,许多 pthreads
已创建。当线程引发异常时,它不会被 main
捕获。这是标准行为吗?
As part of the shared objects execution, many pthreads
are created. When a thread throws an exception, it is not caught by main
. Is this the standard behavior? How can I catch all exceptions, no matter what thread they are thrown from?
推荐答案
将如何处理所有异常? ()捕获线程抛出的异常?
Will main() catch exceptions thrown from threads?
否
当线程引发异常时,不会被main捕获。这是标准行为吗?
When a thread throws an exception, it is not caught by main. Is this the standard behavior?
是的,这是标准行为。
要捕获源自线程 X
的异常,您必须进行 try
- catch线程
子句(例如,围绕线程函数中的所有内容,类似于您在 X
中的 main
)。
To catch an exception originating in thread X
, you have to have the try
-catch
clause in thread X
(for example, around everything in the thread function, similarly to what you already do in main
).
有关相关问题,请参见如何在线程之间传播异常?
For a related question, see How can I propagate exceptions between threads?
这篇关于main()会捕获线程抛出的异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!