匿名线程的异常

发布于 2024-09-30 03:51:10 字数 684 浏览 5 评论 0原文

我试图用异常包围我的代码,这样我就不会出现任何内存泄漏。 我尝试了以下代码,由于某种原因,异常没有被处理,并且出现运行时错误。

一些代码:

   try
        {
            methodA();
        } catch (Throwable th)
        {
            MsgProxyLogger.error(TAG, th.getMessage());
        }
    }


 protected void methodA()
 {


            Thread disptacherThread = new Thread()
            {

                @Override
                public void run()
                {
                    dispatcher.dispatch(existingMessagesArr);
                }
            };
            disptacherThread.start();
   }  

现在如果线程内发生一些运行时异常,它不会被捕获在 throable 子句中?

这是为什么?匿名线程正在取消 catch 子句吗?

谢谢,

雷。

im trying to surround my code with exceptions all over so I wont have any memory leaks.
I tried the following code, and for some reason the exception isnt being handled and i get runtime error.

some code:

   try
        {
            methodA();
        } catch (Throwable th)
        {
            MsgProxyLogger.error(TAG, th.getMessage());
        }
    }


 protected void methodA()
 {


            Thread disptacherThread = new Thread()
            {

                @Override
                public void run()
                {
                    dispatcher.dispatch(existingMessagesArr);
                }
            };
            disptacherThread.start();
   }  

Now if some runtime exception occurse inside the thread, it wont be caught in the throable clauses ?

why is that? does Anonymous thread is canceling the catch clauses?

Thanks,

ray.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-10-07 03:51:10

不幸的是没有。

try-catch 块只能捕获 Thread.Start() 方法(即内存不足)。但是一旦线程启动,或者更好的是,已安排启动,您将失去对它的控制。

如果 unhandlex 异常超出线程范围,您的 JVM 将崩溃。你应该用 try-catch 包围你的内部匿名方法。

希望对您有所帮助。

Unfortunately not.

The try-catch block is able to catch only up to Thread.Start() method (ie. out of memory). But once the thread is started, or better, has been scheduled for start, you will lose control of it.

If an unhandlex exception goes outside the thread scope your JVM will crash. You should surround your inner anonymous method with try-catch.

Hope to have been of help.

沫离伤花 2024-10-07 03:51:10

现在如果线程内部发生一些运行时异常,它不会被捕获在 throwable 子句中吗?

这是正确的。 try / catch 只能捕获当前线程抛出的异常。匿名 Runnable 的 run 方法中的代码在不同的线程上执行。

解决方案有两种:

  • run方法中放置try / catch来处理异常。
  • 向子线程或其ThreadGroup 注册一个UncaughtExceptionHandler

顺便说一句,如果您担心异常导致资源泄漏,最好的解决方案是使用以下模式编写代码:

    Resource r = // allocate resource
    try {
        // use resource r
    } catch (...) {
        ...
    } finally {
        // release resource r
    }

简单地捕获并记录异常(如示例代码所示)并不能解决资源泄漏问题。

Now if some runtime exception occurs inside the thread, it wont be caught in the throwable clauses ?

That is correct. A try / catch can only catch exceptions thrown by the current thread. The code in the anonymous Runnable's run method is executed on a different thread.

There are two solutions to this:

  • Put a try / catch in the run method to deal with the exception.
  • Register an UncaughtExceptionHandler either with the child thread, or its ThreadGroup.

Incidentally, if you are concerned with exceptions causing resource leaks, the best solution is to write your code using this pattern:

    Resource r = // allocate resource
    try {
        // use resource r
    } catch (...) {
        ...
    } finally {
        // release resource r
    }

Simply catching and logging exceptions (as your example code does) will not cure a resource leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文