gpt4 book ai didi

java - 跟踪程序中的所有异常

转载 作者:行者123 更新时间:2023-11-30 09:23:50 25 4
gpt4 key购买 nike

我正在编写游戏程序,很少会出现异常。我希望能够记录我在单独线程上遇到的所有异常。我的程序已经是多线程的了。在某些情况下,我使用 try catch,对于那些我可以只为捕获的异常设置一个异常变量。但是我希望能够找出所有线程上抛出的所有异常,而不用将每个语句都放在 try catch 中。为了澄清,我想要异常对象而不仅仅是异常的名称。

最佳答案

你想要 Thread.UncaughtExceptionHandler :

Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.

参见 here了解详情。

关于java - 跟踪程序中的所有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15899559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com