Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

java - what happens to the exception thrown in try block if an expectation is thrown again in finally block?

I am trying to understand what happens to the exception thrown in try block, when an exception occurs again in finally block?

After running code below, I only see exception thrown in finally block. I am trying to understand what happens to the exception in thrown in try block?

        try{
            int i = 10/0;
        }
        finally {
                System.out.println("one");

                int[] a = new int[2];
                a[4] = 0;

                System.out.println("two");
        }
question from:https://stackoverflow.com/questions/65938733/what-happens-to-the-exception-thrown-in-try-block-if-an-expectation-is-thrown-ag

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The exception in the try block gets discarded and forgotten, as specified in the language specification:

  • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:

....

  • If the run-time type of V is not assignment compatible with a catchable exception class of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
  • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
  • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...