2
public class ExceptionHandler {

    public static void main(String[] args) throws FileNotFoundException, IOException {
    // write your code here
        try {
            testException(-5);
            testException(11);
        } catch (FileNotFoundException e){
            System.out.println("No File Found");
        } catch (IOException e){
            System.out.println("IO Error occurred");
        } finally { //The finally block always executes when the try block exits.
            System.out.println("Releasing resources");
            testException(15);
        }
    }

    public static void testException(int i) throws FileNotFoundException, IOException {
        if (i < 0) {
            FileNotFoundException myException = new FileNotFoundException();
            throw myException;
        }
        else if (i > 10) {
            throw new IOException();
        }
    }
}

The output of this code gives

No File Found Releasing resources

Is it possible to have java catch both IOException as well as FileNotFoundException? It seems to be only able to catch the first exception and doesn't catch the IOException

3
  • It's possible, but not that way. You would have to nest the first try/catch in another try block, and to rethrow the FileNotFoundException from the nested catch block. But why would you do that? Just do the same thing in both catch blocks if that's what you want. Commented Apr 7, 2018 at 9:05
  • 1
    first "testException(10);" line never execute as per your code Commented Apr 7, 2018 at 9:08
  • This is a tricky code example that contains several conditions not as easy to see as it seems. I guess that executing it will show in the standard output an exception not being captured (you should have shown it in your question). See my answer to understand what is happening. Commented Apr 7, 2018 at 10:02

2 Answers 2

3

The try block stops at the first exception thrown, therefore the second call of testException() is never executed.

Sign up to request clarification or add additional context in comments.

6 Comments

The second call to testException() is indeed testException(15); and it is actually being executed (see my answer which explains it).
I'm referring to the second one in the try block.
I know, but that call to testException does not throw the IOException anyway (because 10 is not > 10). So that is not the problem of Eneres.
What you say is true but this is also the problem, because even if the second call would satisfy the condition to throw a FileNotFoundException it would never be called because of the exception thrown by the first call in the try block.
It depends on the intention of the programmer. Hopefully he will explain to us :)
|
2

You should enclose your try/catch/finally block inside another try/catch block because your finally block can throw exceptions that must be captured.

Here is how your code works:

  • testException(-5) throws a FileNotFoundException
  • FileNotFoundExceptionis catched by catch (FileNotFoundException e)
  • No File Found is printed into the standard output
  • Then finally block is executed (the testException(10) sentence is not executed).
  • So Releasing resources is printed
  • And testException(15) is executed throwing an IOException that is not captured anyway (the program will be interrupted).

If you remove the throws FileNotFoundException, IOException from your main method the compiler will alert you that an exception is not being captured (the one in the finally block).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.