-2

I'm trying to catch a specific exception and handle it, and then throw a generic exception which executes code to handle any exception. How is this accomplished? This snippet doesn't catch the Exception and so the output is

Exception in thread "main" java.lang.Exception
    at Main.main(Main.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
IOException specific handling

Process finished with exit code 1

The snippet:

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws Exception {
      try {
        throw new IOException();
      } catch (IOException re) {
        System.out.println("IOException specific handling");
        throw new Exception();
      } catch (Exception e) {
        System.out.println("Generic handling for IOException and all other exceptions");
      }
    }
}
newToScala
  • 397
  • 4
  • 16
  • 4
    Possible duplicate of [Exception thrown inside catch block - will it be caught again?](http://stackoverflow.com/questions/143622/exception-thrown-inside-catch-block-will-it-be-caught-again) – Susannah Potts Oct 14 '16 at 17:55
  • In what way is this failing? You reached the `catch` block because the message `"IOException specific handling"` was printed. Then the application exited with an exception because, well, you *threw an exception*. – David Oct 14 '16 at 17:55
  • @David I want the code in the Exception block to be executed. – newToScala Oct 14 '16 at 17:55
  • 2
    @newToScala look at my linked question in the first comment, it explains why/how your code is executing exactly how it should. – Susannah Potts Oct 14 '16 at 17:56
  • @newToScala: The `IOException` block takes precedence because an `IOException` was thrown. Only one `catch` block is used. If you want to catch the `Exception` that you're throwing, you'd need to wrap this whole operation in a try/catch. – David Oct 14 '16 at 17:56
  • @David I want to cascade the exceptions. – newToScala Oct 14 '16 at 17:57
  • @SusannahPotts I am asking how to make the code execute the code in both exception blocks. Not why it works the way it does. – newToScala Oct 14 '16 at 17:57
  • @newToScala You can't do it like that. Not in `Java` at least. And you don't even understand how your code works so it doesn't matter what you want from it. – Susannah Potts Oct 14 '16 at 17:58
  • @newToScala: The result of the entire `try/catch/catch` block is itself an exception. If you want to catch *that* exception, wrap the whole thing in another `try/catch`. – David Oct 14 '16 at 17:59

1 Answers1

1

The Exception which you throw in the catch-block of the IOException is never caught. That's why you had to add "throws Exception" to your main method.

More than one catch-block after the same try behave like an if..else cascade in order to look for the catch-block which is suitable to handle the particular exception.

Embed the whole try..catch in another try..catch block:

try {
  try {
    throw new IOException();
  } catch (IOException re) {
    System.out.println("IOException specific handling");
    throw new Exception();
  }
} catch (Exception e) {
    System.out.println("Generic handling for IOException and all other xceptions");
  }

Usually one embeds the original exception in the new generic exception so that you do not loose information at the final exception handling (e.g. if you want to log the stack trace to identify where exactly the exception occured):

    throw new Exception(re);

and in the final catch-block:

    e.printStackTrace();
Heri
  • 4,368
  • 1
  • 31
  • 51