异步回调中的java捕获异常

xoshrz7s  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(1941)

我有一个回调,它可能抛出一个自定义异常。我试着抛出它,但是它没有被外部范围捕获,编译器也不让我捕获它,它说:“exception is never thrown is the corresponding try block”,尽管它是。
这是我的密码:

  1. public void openAsync(MessageAsyncCallback callback) {
  2. try {
  3. this.sendChannelOpen(this.getChannel(), getChannelOpenData().getFlags(), new MessageAsyncCallback() {
  4. @Override
  5. public void onComplete() throws NanoException {
  6. // INanoPacket message = transport.getMessageByClassName(AudioServerHandshake.class.getName());
  7. INanoPacket message = transport.getMessageByClassName(AudioClientHandshake.class.getName());
  8. Log.info("Got audio server handshake, trying to client-handshake it");
  9. sendClientHandshakeAsync((AudioServerHandshake) message, callback);
  10. }
  11. });
  12. } catch (NanoException e) {
  13. System.exit(-2);
  14. }
  15. }

它不让我抓住 NanoException 编辑:内部 transport.getMessageByClassName 我扔了一个 NanoException .
edit2:这是调用异常的方法:

  1. public INanoPacket getMessageByClassName(String destClassName) throws NanoException {//} throws NanoException {
  2. long startTime = System.currentTimeMillis(); // fetch starting time
  3. INanoPacket message = this.getMessageFromTCPQueue();
  4. while (!(message.getClass().getName().equals(destClassName)) && isRuntimeValid(startTime)) {
  5. this.insertToTCPQueue(message); // put message back in queue
  6. message = this.getMessageFromTCPQueue();
  7. }
  8. if (!(message.getClass().getName().equals(destClassName))) {
  9. // timeout...
  10. throw new NanoException("Couldn't find destination message: " + destClassName);
  11. }
  12. return message;
  13. }

我想处理这个异常,即使在 openAsync 但是在调用 openAsync . 为什么?因为我正在处理来自远程设备的消息,所以它是异步的。我使用某种超时来等待一个特定的消息,如果消息不来,我想重新启动整个程序。

lsmepo6l

lsmepo6l1#

请注意,在您的代码中,您没有调用 onComplete 方法,您正在定义它。
异常将被抛出到代码的单独部分,可能是单独的 Thread (似乎是异步的)。因此,“exception is never thrown is the corresponding try block”消息是正确的,因为在调用 this.sendChannelOpen(...) 方法。
你的 try-catch 语句需要 Package 调用 onComplete 方法。只有通过调用 onComplete 你能指望什么方法 NanoException .
基于注解编辑:如果您需要处理异常,则抛出 getMessageByClassName 你可以在家里做 onComplete 方法,而不是重新引用它。如果你想在别的地方处理,你需要给我们提供 sendChannelOpen 方法或调用回调的位置。
edit2(基于问题编辑):请参阅下面的代码,作为如何在线程之间通信的示例。我已经使用了latch,但是java.util.concurrent中还有其他类可能会有用。顺便说一句,我不想讨论为什么要在nanoexception上重新启动整个应用程序,尽管从该异常中恢复可能还有其他值得考虑的选项。

  1. import java.util.concurrent.CountDownLatch;
  2. class NanoException extends Exception {}
  3. interface MessageAsyncCallback {
  4. void onComplete() throws NanoException;
  5. }
  6. public class AsyncApp {
  7. private static final CountDownLatch errorLatch = new CountDownLatch(1);
  8. public static void main(String[] args) {
  9. new AsyncApp().run();
  10. }
  11. void run() {
  12. sendChannelOpen("something", new MessageAsyncCallback() {
  13. @Override
  14. public void onComplete() throws NanoException {
  15. // the whole try-catch-sleep is not really needed, just to wait a bit before exception is thrown
  16. try {
  17. // not needed, just to wait a bit before exception is thrown
  18. Thread.sleep(5000);
  19. } catch (InterruptedException e) {
  20. throw new NanoException();
  21. }
  22. throw new NanoException();
  23. }
  24. });
  25. try {
  26. System.out.println("This is a main thread and we wait here, while the other thread executes...");
  27. errorLatch.await();
  28. System.out.println("Latch has reached 0, will now exit.");
  29. System.exit(-2);
  30. } catch (InterruptedException e) {
  31. System.out.println("Error in main thread.");
  32. System.exit(-1);
  33. }
  34. }
  35. void sendChannelOpen(String notImportant, MessageAsyncCallback troublesomeCallback) {
  36. runSomethingInSeparateThread(troublesomeCallback);
  37. }
  38. void runSomethingInSeparateThread(MessageAsyncCallback troublesomeCallback) {
  39. new Thread(() -> {
  40. try {
  41. troublesomeCallback.onComplete();
  42. } catch (NanoException e) {
  43. System.out.println("You can catch it here, and do system exit here or synchronize with main Thread as below");
  44. errorLatch.countDown();
  45. }
  46. }).start();
  47. }
  48. }
展开查看全部

相关问题