【问题标题】:Why such output is coming?为什么会有这样的输出?
【发布时间】:2014-10-14 07:46:35
【问题描述】:

这可能很简单,但我不明白为什么输出是 1 4。第 9 行的 return 语句的功能是什么?

public static void main(String[] args) {
    try{
        f();
    } catch(InterruptedException e){
        System.out.println("1");
        throw new RuntimeException();
    } catch(RuntimeException e){
        System.out.println("2");
        return;                    \\ Line 9
    } catch(Exception e){
        System.out.println("3");
    } finally{
        System.out.println("4");
    } 
        System.out.println("5");
}   
        static void f() throws InterruptedException{
            throw new InterruptedException("Interrupted");
    }

提前致谢。

【问题讨论】:

  • 运行时异常没有被捕获,因为没有try并且第9行没有执行。
  • 另外 5 也不会被打印,因为RuntimeException 是一个未经检查的异常,因此在内部被捕获(没有明确的try-catch 块)并在System.out.println("5"); 之前终止程序

标签: java exception exception-handling try-catch


【解决方案1】:

你的function f() 抛出InterruptedException,它被第一个catch 块捕获(因此它打印1),但是这个catch 块不能抛出其他异常(如果它不是你的方法抛出的),因此,没有其他catch 块可以捕获您的异常,因此 finally 被执行(最终在所有情况下执行,除了那些愚蠢的无限循环情况)。可以参考Exception thrown inside catch block - will it be caught again?

希望对你有帮助。

总结一下,你可以从 try 块中抛出任何异常,它会被捕获(如果有一个好的 catch 块)。但是从 catch 块中,只有那些你的方法抛出的异常可以被抛出(并因此被捕获)。

如果您从 catch 块中抛出您的方法未抛出的异常,则意义较小并且不会被捕获(就像您的情况一样)。

【讨论】:

    【解决方案2】:

    如您所见,f() 抛出 InterruptedException,因此它会首先打印第一个 catch 块内的 1,然后 finally 会执行,因此它会打印 4.

    【讨论】:

      【解决方案3】:

      打印 1 是因为 f() 抛出了一个 InterruptedException。因为异常是在第一个 catch 块中处理的,所以它不再在下面的其他异常块中处理。 finally 语句总是运行,所以 4 也被打印出来了。

      【讨论】:

        【解决方案4】:
        try{
            f();
        } catch(InterruptedException e){
            System.out.println("1");
            throw new RuntimeException(); // this RuntimeException will not catch by 
                                          //following catch block
        } catch(RuntimeException e){
        

        你需要改变你的代码来捕捉它。

         try{
                f();
            } catch(InterruptedException e){
                System.out.println("1");
                try {
                    throw new RuntimeException();// this RuntimeException will catch 
                                                 // by the following catch 
                }catch (RuntimeException e1){
                    System.out.println("hello");
                }
            } catch(RuntimeException e){
                System.out.println("2");
                return;
            } catch(Exception e){
                System.out.println("3");
            } finally{
                System.out.println("4");
            }
            System.out.println("5");
        

        然后你的输出”

          1
          hello // caught the RunTimeException
          4 // will give you 2,but also going to finally block then top element of stack is 4
          5 // last print element out side try-catch-finally
        

        【讨论】:

          【解决方案5】:

          f() 抛出一个InterruptedException,所以你得到1。 finally 总是在最后被调用,所以你得到4

          【讨论】:

            【解决方案6】:

            当 f() 抛出 InterruptedException 执行时,

            catch(InterruptedException e){
                    System.out.println("1");
                    throw new RuntimeException();
            }
            

            打印 --> 1

            最后在退出程序前被执行,

            finally{
                    System.out.println("4");
            }
            

            打印 --> 4

            返回后的代码不会执行,但最终会被执行。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-09-21
              • 2013-10-07
              • 1970-01-01
              • 2015-05-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-04-01
              相关资源
              最近更新 更多