C++ main() 会捕获线程抛出的异常吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7730502/
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
Will main() catch exceptions thrown from threads?
提问by steveo225
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).
我有一个非常大的应用程序,它动态加载共享对象并在共享对象中执行代码。作为预防措施,我在main
. 我为 3 件事创建了一个捕获:(myException
内部异常)std::exception
、 和...
(捕获所有异常)。
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?
作为共享对象执行的一部分,pthreads
创建了许多。当线程抛出异常时,它不会被 捕获main
。这是标准行为吗?我怎样才能捕获所有异常,不管它们是从哪个线程抛出的?
回答by NPE
Will main() catch exceptions thrown from threads?
main() 会捕获线程抛出的异常吗?
No
不
When a thread throws an exception, it is not caught by main. Is this the standard behavior?
当线程抛出异常时,它不会被 main 捕获。这是标准行为吗?
Yes, this is standard behaviour.
是的,这是标准行为。
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
).
要捕获源自 thread 的异常X
,您必须在 thread中有try
-catch
子句X
(例如,围绕 thread 函数中的所有内容,类似于您在 中所做的main
)。
For a related question, see How can I propagate exceptions between threads?
有关相关问题,请参阅如何在线程之间传播异常?
回答by bmargulies
Your question is asking for something that isn't conceptually possible.
你的问题是要求一些概念上不可能的东西。
Try blocks are defined as dynamic constructs of the stack. A try block catches exceptions thrown by code reached dynamically, by call, from its contents.
Try 块被定义为堆栈的动态构造。try 块从其内容中捕获由通过调用动态到达的代码抛出的异常。
When you create a new thread, you create a brand-new stack, that is not at all part of the dynamic context of the try block, even if the call to pthread_create is inside the try.
当你创建一个新线程时,你创建了一个全新的堆栈,它根本不是 try 块的动态上下文的一部分,即使对 pthread_create 的调用在 try 内部。
回答by Brent Arias
No, main will not catch exceptions thrown from other threads. You would need to use a non-standard, platform specific facility that addresses unhandled exceptions in order to aggregate the handling the way you are describing.
不, main 不会捕获从其他线程抛出的异常。您需要使用非标准的、特定于平台的工具来处理未处理的异常,以便按照您描述的方式聚合处理。
When I build such applications, I make sure each active object has its own top-level exception handling block, precisely to prevent the entire application from exploding if one thread fails. Using a platform-specific catch all I think begs for your overall code / solution to be sloppy. I would not use such a thing.
当我构建这样的应用程序时,我确保每个活动对象都有自己的顶级异常处理块,正是为了防止在一个线程失败时整个应用程序爆炸。使用特定于平台的捕获所有我认为要求您的整体代码/解决方案草率。我不会使用这样的东西。
回答by Dabbler
Consider that throwing an exception unwinds the stack. Each thread has its own stack. You will have to place a try/catch block in each thread function (i.e. in the entry point of each thread).
考虑抛出异常会展开堆栈。每个线程都有自己的堆栈。您必须在每个线程函数中(即在每个线程的入口点)放置一个 try/catch 块。