articles

Home / DeveloperSection / Articles / Exception Handling in Java: final Re-throw and Exceptions in Overriding Methods

Exception Handling in Java: final Re-throw and Exceptions in Overriding Methods

Samuel Fernandes1980 23-May-2016
Difference Between the throw and throws Keywords

As we can make out from the example of preceding post :

·    The throw keyword (note the singular form) is used to force an exception.

·     It can also be used to pass a customized message to the error-handling code.

·     For example, the following statement throws a custom message in the ArithmeticException:

 new ArithmeticException("Attempt to divide by zero");

·       The throws keyword is used when we know that a particular exception may be thrown or whenever we want to pass a possible exception.

The final Re-throw in Java SE 7

We have seen so far that to throw an exception higher up the caller hierarchy, our method must declare that exception type in the throws clause of its declaration. Now, consider a situation where our method may want to throw two different types of exceptions, as shown in the following code snippet:

public void compute() throws IOException, ParseException {

       try {                   // code which may generate IOException, ParseException         } catch (Exception e){                   throw e;         } }

 However, this code will not compile because we are trying to throw a more
generic exception.

For the code to compile, we will need to add the Exception type in the throws clause. This, too, will not compile if the compute method overrides a method that does not declare throwing the Exception type (this is explained in the next post). Java SE 7 solves this problem when we add a final keyword in the catch block, as follows:

catch (final Exception e){

Using the final keyword in the catch block allows us to throw the exact exception subtype that occurred. For example, if IOException occurs, then IOException would be thrown; if ParseException occurs, then ParseException would be thrown. The final keyword allows us to throw the exact exception that occurred without the need to add the Exception type to the method signature.

Declaring Exceptions in Overridden Methods

We have already learnt the benefits of creating inheritance hierarchies to produce highly structured code. When we extend the classes, we override some of their methods in their subclasses. What if some of these methods are already declared to throw a few exception types?

When we override these methods, we must observe certain rules:

·An overriding method must throw exceptions of the same type as the exceptions being thrown by the overridden method.

·An overriding method may throw exceptions that are subclasses of the exceptions being thrown by the overridden method.

·An overriding method cannot throw a superclass exception of an exception declared by an overridden method.

·In case of an overridden method throwing multiple exceptions, an overriding method must throw a proper subset of exceptions thrown by the overridden method.


Updated 30-Nov-2017

Leave Comment

Comments

Liked By