0
    try {
        Image image = new Image("MyComputer/Documents/Photos/estambul.jpg");

        try {
            //...

            image.print();

            throw new NullPointerException();
        } finally {
            System.out.println("FINALLY");
            image.close();
            throw new IOException();
        }
    } catch (IOException ex){
        System.out.println("IO_EXCEPTION");
        System.out.println(ex.getMessage());
    }

    System.out.println("THREAD IS CONTINUE");

NullPointerException is not thrown here. However, there is no catch block that catches it. It looks like IOException is squashing NullPointerException. What is your comment? I did not understand.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Backwindow
  • 21
  • 4

1 Answers1

1

NullPointerException is not thrown here.

Actually, it is ... assuming that the code reaches the statement where you explicitly throwing it.

But the thing is that the inner try has a finally block. And in the finally block you are explicitly throwing an IOException. This is going to replace the original exception.

The behavior is specified in JLS 14.20.2. The text that governs youw example is:

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

...

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

It looks like IOException is squashing NullPointerException.

Sort of. However, we use the informal term squashing to refer to the practice of catching and ignoring exceptions.


Then it can only handle one exception at runtime.

That is correct. At any point in time there can be at most one "abrupt termination reason" for each thread.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216