简体   繁体   English

您如何在Java中记录未捕获的异常?

[英]How do you log an uncaught exception in Java?

My client is testing my beta phase Java application on a data that he cannot provide me with (privacy reasons). 我的客户正在对他无法提供给我的数据(隐私原因)测试我的beta阶段Java应用程序。 Although I implemented the handling of most exceptions, application crashes on rare occasions. 尽管我实现了大多数异常的处理,但是在极少数情况下应用程序会崩溃。

Can I log the stacktrace of an uncaught exception to a file so that tester can provide me with it(eg NullPointerException )? 我可以将未捕获的异常的堆栈跟踪记录到文件中,以便测试人员可以向我提供它(例如NullPointerException )吗?

设置一个Thread.UncaughtExceptionHandler打印到指定的文件。

If the main thread crashes, the JVM will print a stacktrace automatically. 如果主线程崩溃,JVM将自动打印一个堆栈跟踪。

For other threads, you can use setUncaughtExceptionHandler . 对于其他线程,可以使用setUncaughtExceptionHandler

This is exactly the thing what UncaughtExceptionHandler concept is about . 这正是UncaughtExceptionHandler概念的UncaughtExceptionHandler

Basically you are able to register a piece of code to be executed when the exception reaches the top of the stack. 基本上,您可以注册一条代码,以在异常到达堆栈顶部时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM