JDK 7 Locating multiple types of exceptions and throwing exceptions with improved type checking

Before Java 7, if we had to rebuild a method exception, we would need to do one of two 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 second -

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 such a way that you can catch a wide level of exceptions from exceptions and still keep narrow exceptions in the method definition, just like the code below.

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

      

The Java SE 7 compiler can determine that an exception thrown by a throw statement must have come from a try block, and the only exceptions thrown by a try block can be FirstException and SecondException. Although the exception parameter in the catch clause, e, is of type Exception, the compiler can determine that it is an instance of FirstException or SecondException. This parsing is disabled if the catch parameter is assigned to a different value in the catch block. However, if the catch parameter is set to a different value, you must specify the Exception type 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 types of exceptions in a catch clause and reverse engineer the exception handled by that catch block, the compiler checks that the type of the repeated exception satisfies 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 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 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 could not theoretically validate the 3rd and 4th point. Can anyone explain me in context with the above code?

+3


source to share


1 answer


Consider the following situation:

Your application provides one of the exceptions FirstException

, SecondException

,Exception

Exception

- a supertype FirstException

, SecondException

because they expand Exception

.

This should also be applied SecondException extends FirstException

. Therefore, it FirstException

is a supertype SecondExeption

and a SecondException

subtype FirstException

.

We now have a method that always emits 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 meaning?

+2


source







All Articles