如何诊断“未处理的异常”在Java中?

发布于 2024-08-11 09:37:44 字数 138 浏览 4 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(5

月光色 2024-08-18 09:37:44

实现 Thread.UncaughtExceptionHandler 接口并使用 setDefaultUncaughtExceptionHandler() 来设置它。

示例程序作为礼貌。如果您使用多个线程,您也可以在您怀疑是罪魁祸首的线程上设置处理程序。

public class Test {
  public static void main(String args[]) {
      new Test();
  }
  public Test() {
      Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
      // or if a default handler is set you can also use setUncaughtExceptionHandler
      // check the javadocs for the specifics
      throw new RuntimeException ("You can't get me or can you?");
  }
  class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
      public void uncaughtException(Thread t, Throwable e) {
          System.err.println ("Uncaught exception by " + t + " caught:");
          e.printStackTrace();
      }
  }
}

Implement the Thread.UncaughtExceptionHandler interface and use setDefaultUncaughtExceptionHandler() 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.

public class Test {
  public static void main(String args[]) {
      new Test();
  }
  public Test() {
      Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
      // or if a default handler is set you can also use setUncaughtExceptionHandler
      // check the javadocs for the specifics
      throw new RuntimeException ("You can't get me or can you?");
  }
  class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
      public void uncaughtException(Thread t, Throwable e) {
          System.err.println ("Uncaught exception by " + t + " caught:");
          e.printStackTrace();
      }
  }
}
无言温柔 2024-08-18 09:37:44

您可以在代码段周围放置一个 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.

青衫负雪 2024-08-18 09:37:44

但是当我运行程序时,设备
显示“未处理的异常”并询问
我是否关闭应用程序。

首先,您需要在代码中找到生成此消息的位置。听起来该应用程序有一个 GUI 或其他什么,所以它可能是某种对话框。

接下来,找到代码中导致创建消息/对话框的位置。它可能是

  • 捕获 ExceptionThrowabletry / catch 块,或者是
  • UncaughtExceptionHandler。

无论哪种情况,下一步都是添加一些代码以使应用程序输出未捕获异常的堆栈跟踪。假设 ex 持有对异常对象的引用:

    ex.printStackTrace();

将向标准错误流写入堆栈跟踪;例如“控制台”。 (如果您找不到“控制台”输出的去向,还有更多重量级解决方案。)

But as I run the program the device
shows "Unhandled Exception" and asks
me whether to close the app.

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

  • a try / catch block that catches Exception or Throwable, or
  • an UncaughtExceptionHandler.

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:

    ex.printStackTrace();

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.)

时间你老了 2024-08-18 09:37:44

听起来好像你在某个地方发生了未经检查的 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.

開玄 2024-08-18 09:37:44

如果你没有堆栈跟踪,你就不能做很多事情。

如果它实际上是由另一方引起的,并且您收到此消息作为消息,那么您应该向另一方询问堆栈跟踪或类似信息。

编辑:您当然应该确保您自己的代码不会吞咽异常,例如:

} catch (Exception e) {
    // Do nothing.
}

} catch (Exception e) {
    System.out.println("Error");
}

} catch (Exception e) {
    System.out.println(e.toString());
}

要从异常中获取最多信息,您至少应该这样做:

} catch (Exception e) {
    e.printStackTrace();
}

或 只是

} catch (Exception e) {
    throw e;
}

希望这会有所帮助。

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:

} catch (Exception e) {
    // Do nothing.
}

or

} catch (Exception e) {
    System.out.println("Error");
}

or

} catch (Exception e) {
    System.out.println(e.toString());
}

To get the most information out of exceptions, you should do at least:

} catch (Exception e) {
    e.printStackTrace();
}

or just

} catch (Exception e) {
    throw e;
}

Hope this helps.

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