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

标签 java exception try-catch

我正在编写一个游戏程序,很少会遇到异常。我希望能够在单独的线程上记录我遇到的所有异常。我的程序已经是多线程的了。在某些情况下,我使用 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/

相关文章:

python - 如果 try 语句在 python 中成功,则运行代码

java - 如何计算考虑到日节省类次的日期之间的天数?

python - 测试装饰器引发的异常的好方法是什么?

javascript - 为什么 'error' 变量重新分配在功能范围内不是 catch{} block ?

c# - 在服务边界记录 WCF 错误

.net - 重试临时异常 .Net

c# - 读取 XML 时在 IEnumerable 中构建 Try-Catch

java - 如何使用 Log4j 创建基于进程的日志文件?

java - JSP Tomcat 不良资源

java - if 语句中的 sendKey 问题