Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK 7 Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking

Prior to Java 7, if we had to rethrow an exception from a method, then we will have to do either of the 2 ways,

public void rethrowException(String exceptionName) throws FirstException, SecondException{
    try {
      if (exceptionName.equals("First")) {
        throw new FirstException();
      } else {
        throw new SecondException();
      }
    } catch (FirstExceptione) {
      throw e;
    }catch (SecondException) {
      throw e;
    }
  }

and the second one being,

public void rethrowException(String exceptionName) throws Exception {
    try {
      if (exceptionName.equals("First")) {
        throw new FirstException();
      } else {
        throw new SecondException();
      }
    } catch (Exception e) {
      throw e;
    }
  }

As per my understanding, New Java 7.0 has improved in a way that you can catch wide level of exception of exceptions and still keeps the narrow exceptions in the method definition, just like below code,

public void rethrowException(String exceptionName)
  throws FirstException, SecondException {
    try {
      // ...
    }
    catch (Exception e) {
      throw e;
    }
  }

The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException. This analysis is disabled if the catch parameter is assigned to another value in the catch block. However, if the catch parameter is assigned to another value, you must specify the exception type Exception in the throws clause of the method declaration.

From Oracle docs,

In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:

1) The try block is able to throw it.
2) There are no other preceding catch blocks that can handle it.
3) It is a subtype or supertype of one of the catch clause's exception parameters.
4) In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of   
the catch clause's exception parameters. A compiler from a release prior to Java SE 7 generates 
the error, "unreported exception Exception; must be caught or declared to be thrown" at the 
statement throw e. The compiler checks if the type of the exception thrown is assignable to any 
of the types declared in the throws clause of the rethrowException method declaration. However, 
the type of the catch parameter e is Exception, which is a supertype, not a subtype, of 
FirstException andSecondException.

I failed to undertand the 3rd and the 4rth point theoritically. Can someone explain me in context with the code mentioned above?

like image 899
Farhan Shirgill Ansari Avatar asked Feb 25 '26 22:02

Farhan Shirgill Ansari


1 Answers

Consider following situation:

Your application throws one of the exceptions FirstException, SecondException, Exception

Exception is the supertype of FirstException, SecondException because they extend Exception.

It should also apply that SecondException extends FirstException. So FirstException is a supertype of SecondExeption and SecondException a subtype of FirstException.

Now we have a method which always throws SecondException.

First Case:

try {
[...]
} catch(SecondException se) {
// Exception gets always caught in here
[...]
} catch(FirstException fe) {
[...]
} catch(Exception e) {
[...]
}

Second Case:

try {
[...]
} catch(Exception e) {
// Exception gets always caught in here
// because Exception is supertype of all other Exception
[...]
} catch(FirstException fe) {
[...]
} catch(SecondException se) {
// is never called.
[...]
} 

Do you see the point?

like image 196
sxleixer Avatar answered Feb 28 '26 12:02

sxleixer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!