开发者社区> 问答> 正文

在“内部”方法内引发异常后,如何停止Java方法执行?

我写了一个描述我的问题的例子。我有一个简单的例外:

public class TryToSpeakException extends RuntimeException {

    public TryToSpeakException(String message) {
        super(message);
    }
}

然后,我有一系列方法互相调用:

public class TryToSpeak {

    public static void speakToPerson1() {
        throw new TryToSpeakException("Person 1 don't wanna speak to anyone.");
    }

    public static void speakToPerson2() {
        try {
            speakToPerson1();
        } catch (Exception e) {
            System.out.println("speakToPerson2 caused exception");
            e.printStackTrace();
        }
    }

    public static void speakToPerson3() {
        try {
            speakToPerson2();

        } catch (Exception e) {
            System.out.println("speakToPerson3 caused exception");
            e.printStackTrace();
        }
    }

    static void keepSilentToPerson() {
        System.out.println("Keeping silent to person 1");
    }

    public static void communicate() {
        try {
            speakToPerson3();

            keepSilentToPerson(); // Why it reaches this part?
        } catch (Exception e) {
            System.out.println("Communication exception.");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        communicate();
    }
}

从最底部开始的方法将引发异常。在communicate()调用方法之后的方法中speakToPerson3(),执行继续进行。我需要的是-停止执行,这样keepSilentToPerson()就永远不会达到方法的调用,并给我消息"Communication exception."。

这就是我现在在控制台中得到的:

speakToPerson2 caused exception
exceptions.TryToSpeakException: Person 1 don't wanna speak to anyone.
    at exceptions.TryToSpeak.speakToPerson1(TryToSpeak.java:7)
    at exceptions.TryToSpeak.speakToPerson2(TryToSpeak.java:12)
    at exceptions.TryToSpeak.speakToPerson3(TryToSpeak.java:22)
    at exceptions.TryToSpeak.communicate(TryToSpeak.java:36)
    at exceptions.TryToSpeak.main(TryToSpeak.java:46)
Keeping silent to person 1

但是,如果我speakToPerson2()从这样的Main方法调用:

public static void speakToPerson1() {
    throw new TryToSpeakException("Person 1 don't wanna speak to anyone.");
}

public static void speakToPerson2() {
    try {
        speakToPerson1();

        keepSilentToPerson();
    } catch (Exception e) {
        System.out.println("speakToPerson2 caused exception");
        e.printStackTrace();
    }
}

static void keepSilentToPerson() {
    System.out.println("Keeping silent to person 1");
}

public static void main(String[] args) {
    speakToPerson2();
}

我得到一个明显的结果:

```js

speakToPerson2 caused exception exceptions.TryToSpeakException: Person 1 don't wanna speak to anyone. at exceptions.TryToSpeak.speakToPerson1(TryToSpeak.java:7) at exceptions.TryToSpeak.speakToPerson2(TryToSpeak.java:12) at exceptions.TryToSpeak.main(TryToSpeak.java:26)

keepSilentToPerson() 未达到。

我究竟做错了什么?

展开
收起
垚tutu 2019-11-28 22:51:49 884 0
1 条回答
写回答
取消 提交回答
  • #include

    speakToPerson2()正在捕获异常,但是您也想communicate()捕获它。本质上,speakToPerson2()是抑制异常,因此没有其他方法可以看到它。

    您需要做的是将其重新扔入(在speakToPerson2()和中speakToPerson3())。这被称为传播异常。

    即:

    public static void speakToPerson2() {
        try {
          speakToPerson1();
        } catch (Exception e) {
          System.out.println("speakToPerson2 caused exception");
          e.printStackTrace();
          throw e;
        }
      }
    
      public static void speakToPerson3() {
        try {
          speakToPerson2();
    
        } catch (Exception e) {
          System.out.println("speakToPerson3 caused exception");
          e.printStackTrace();
          throw e;
        }
      }
    
    2019-11-28 22:52:07
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载