0

In Java, if a general exception is caught and rethrown, will outer methods still be able to catch specific exceptions?

In other words, can I do this:


try {
    try {
         //...
    } catch (Exception e) {
         //...
        throw e;
    }
} catch (SpecificException e) {
     //...
}
Anonymous
  • 3,334
  • 3
  • 35
  • 50

3 Answers3

2

re-throwing an exception does not change anything about it (it's still the same object originally thrown).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
2

While jtahlborn answer is correct, there is one more appreciation: the compiler will see that you are throwing an exception of the generic type (even if at runtime it can be only of the specific class) and will force you to declare the generic exception in the method header.

private void test() throws FileNotFoundException {
    try {
        throw new FileNotFoundException("Es una exception");
    } catch (IOException e) {
        throw e; <-- Error because the method only throws
                       FileNotFoundException, not IOException
    }
}

e is indeed FileNotFoundException, but as it is declared as IOException the compiler works with the broader class. What you can do is "cast" the exception.

        throw (FileNotFoundException) e;
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • But, if you're catching multiple exceptions, then you can't know which one to cast it to. – Anonymous Jan 07 '12 at 04:07
  • 1
    @DougTreadwell if you're catching multiple exceptions, then you're supposed to actually **catch them**. that's why the compiler won't allow this. – davogotland Jan 07 '12 at 04:10
  • then, either check the type with `instanceof` and do the apropiate cast, or stick to declaring the generic exception in the method `throws` clause (in parent methods, you can stil check which was the original exception - but you will have to cover the "generic" exception too, because the compiler can't decide it is not needed). Anyway you are doing it too complicated, either the exceptions must be treated the same by the calling method (then stick to the generic one) or they are not (declare the different specific exceptions in `throws`) – SJuan76 Jan 07 '12 at 04:15
0

Eclipse marks the "throw e" in the inner catch as an unhandled exception, BUT it does catch the exception because when I run this it prints "It worked!". Thanks @jtahlborn. Unfortunately this means that there will still need to be an unnecessary try/catch block somewhere.


public class Tester {

    public static void main(String[] args) {
        try {
            try {
                throw new SpecificException("Test!");
            } catch (Exception e) {
                throw e;
            }
        } catch (SpecificException e) {
            System.out.println("It worked!");
        }
    }

}
Anonymous
  • 3,334
  • 3
  • 35
  • 50
  • 2
    how is that unfortunate? `catch(Exception e)` will catch any exception derived from the class `Exception` (which should be all). and then you're re-throwing it, and only dealing with some specific exception after that. java shouldn't allow that! if you want some exception to be able to actually kill the application, then you should use a runtime exception, since those don't have to be caught. – davogotland Jan 07 '12 at 04:08
  • Exceptions are caught based on the runtime type, but the compiler has to decide on static information alone for its warnings and errors. Certainly in your case we could still distinguish that catch `Exception` can only catch `SpecificException` but for any real problem that's a hard problem. But you shouldn't catch more than you have to anyhow. – Voo Jan 07 '12 at 05:44