如何诊断“未处理的异常”在Java中?
在 Java 程序中,我目前在程序中的某个点收到“未处理的异常”,但我似乎无法确定生成该异常的位置。
由于程序包含处理发送和接收的无线数据字节的流,因此调试代码也很困难。我似乎无法用调试器来模拟它。
我应该采取什么策略来定位异常?
In a Java program I am currently getting "Unhandled Exception" at a certain point in the program and I can't seem to determine the location where this is being generated.
Its also difficult to debug the code as the program contains streams that handle wireless data bytes sent and received. I can't seem to simulate that with a debugger.
What strategy should I adopt to locate the Exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实现 Thread.UncaughtExceptionHandler 接口并使用 setDefaultUncaughtExceptionHandler() 来设置它。
示例程序作为礼貌。如果您使用多个线程,您也可以在您怀疑是罪魁祸首的线程上设置处理程序。
Implement the
Thread.UncaughtExceptionHandler
interface and usesetDefaultUncaughtExceptionHandler()
to set it.Sample program as courtesy. If you use multiple threads you also could just set the handler on threads you suspect to be the culprits.
您可以在代码段周围放置一个 try-catch(Exception ex) 块,然后将其移动/收紧该块,并让它记录抛出的异常。只要没有太多代码,您应该能够在几次运行中找到它。
You could put a try-catch(Exception ex) block around sections of code and move it around/tighten the block and have it log the exception being thrown. As long as there isn't too much code you should be able to track it down in a few runs.
首先,您需要在代码中找到生成此消息的位置。听起来该应用程序有一个 GUI 或其他什么,所以它可能是某种对话框。
接下来,找到代码中导致创建消息/对话框的位置。它可能是
Exception
或Throwable
的try
/catch
块,或者是UncaughtExceptionHandler。
无论哪种情况,下一步都是添加一些代码以使应用程序输出未捕获异常的堆栈跟踪。假设
ex
持有对异常对象的引用:将向标准错误流写入堆栈跟踪;例如“控制台”。 (如果您找不到“控制台”输出的去向,还有更多重量级解决方案。)
First you need to find the place in the code that is generating this message. It sounds like the app has a GUI or whatever, so it is possible a Dialog of some kind.
Next, find the place in the code that is causing the message/dialog to be created. It is likely to be either
try
/catch
block that catchesException
orThrowable
, orUncaughtExceptionHandler
.In either case, the next thing is to add some code to cause the app to output a stacktrace for the uncaught exception. Assuming that
ex
holds a reference to the exception object:will write a stack trace to the standard error stream; e.g. the "console". (There are more heavy-weight solutions if you cannot find where "console" output goes to.)
听起来好像你在某个地方发生了未经检查的 RuntimeException 。您可以在 main() 方法中使用 try { } catch(Throwable t) { t.printStackTrace(); 轻松尝试它或者
,如果您使用 Eclipse 之类的 IDE 远程调试它,则可以将其设置为使用“在未捕获的异常上挂起”在 Java 异常断点上触发调试器。一些文档
Sounds like you've got an unchecked RuntimeException happening somewhere. You could easily try it in your main() method with try { } catch(Throwable t) { t.printStackTrace(); }
Or if you remotely debug it with an IDE like Eclipse, you can set it up to trigger the debugger on a Java exception breakpoint with "Suspend on uncaught exceptions". Some docs here.
如果你没有堆栈跟踪,你就不能做很多事情。
如果它实际上是由另一方引起的,并且您收到此消息作为消息,那么您应该向另一方询问堆栈跟踪或类似信息。
编辑:您当然应该确保您自己的代码不会吞咽异常,例如:
或
或
要从异常中获取最多信息,您至少应该这样做:
或 只是
希望这会有所帮助。
If you don't have a stacktrace, you can't do much.
If it is actually caused at the other side and you received this as a message, then you should ask the other side for a stacktrace or similar information.
Edit: you should of course ensure that your own code isn't swallowing exceptions like as:
or
or
To get the most information out of exceptions, you should do at least:
or just
Hope this helps.