后台线程异常 [英] Exceptions of background threads

查看:120
本文介绍了后台线程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个在多线程环境中工作的库,但是这些函数可能会抛出线程异常,所以现在我该如何处理此类异常??

谢谢,
Dan

I have a library written to work in multi-threading environment, but the functions may throw exceptions from threads, so now how can i handle such exceptions??

Thanks,
Dan

推荐答案

阅读 [ ^ ].关于StackOverflow的小而有效的答案.希望对您有所帮助.
Read this[^]. Small, but effective answer on StackOverflow. Hope it helps.


这并不难理解.让我们想一想.

每个线程都在其自己的堆栈中工作.异常在堆栈上也起作用:抛出的异常将传播到同一线程上最接近的try语句时光倒流"的点(线程展开).因此,您需要在每个线程的堆栈顶部捕获所有异常,以避免线程不受控制地终止.同时,重要的是不要逾期捕获异常.作为事项或规则,切勿在没有针对所捕获的异常类型进行特定异常处理的级别上捕获异常.同样重要的是,不要阻止异常的传播.此规则有相对罕见的排除方法,主要是作为一些无法修补的不良"代码的变通办法.

请查看我过去有关该主题的答案的更多详细信息:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理? [ throw. .then ... rethrowing [ ^ ].

—SA
This is not to hard to understand. Let''s think.

Each thread works in its own stack. Exceptions also work on stack: the thrown exception is propagated to the point of the nearest try statement "back in time" on the same thread (thread unwinding). Therefore you need to catch all exceptions on the top of the stack of each thread, to avoid uncontrolled termination of threads. At the same time, it''s very important not to overdue catching exceptions; as a matter or rule, an exception should never be caught at the level where there is not specific exception handling for the type of exception being caught. It''s equally important not to block propagation of exception. There are relatively rare exclusions from this rule, mostly as a work-around of some "bad" code inaccessible for patching.

Please see more detail in my past answers on the topic:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

—SA


这篇关于后台线程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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