Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare method to throw an exception and subclass of this exception

Is it meaningful to declare a method to throw an exception and a subclass of this exception, e.g. IOException and FileNotFoundException?

I guess that it is used in order to handle both exceptions by a caller method differently. However, is it possible to handle both exceptions if the method throws only the most generic i.e IOException?

like image 990
kon psych Avatar asked Jun 20 '12 09:06

kon psych


People also ask

How do you declare a method that throws an exception?

The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code. The throws keyword is used in a method signature and declares which exceptions can be thrown from a method.

Can we throw exception in subclass?

Subclass method cannot throw the exception which is a superclass of the superclass method's exception. Subclass method can throw any unchecked or runtime exception.

How do you create an exception subclass?

To create our own exception class simply create a class as a subclass of built-in Exception class. We may create constructor in the user-defined exception class and pass a string to Exception class constructor using super(). We can use getMessage() method to access the string.

Is a method that throws exception inherited in a subclass?

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.


2 Answers

However, is it possible to handle both exceptions if the method throws only the most generic i.e IOException?

Absolutely. You can still catch them separately:

try {
  methodThrowingIOException();
} catch (FileNotFoundException e) {
  doSomething();
} catch (IOException e) {
  doSomethingElse();
}

So it makes no difference to what the caller can do if the method declares both - it's redundant. However, it can emphasize exceptions that you might want to consider. This could be done better in Javadoc than just the throws declaration.

like image 50
Jon Skeet Avatar answered Oct 29 '22 13:10

Jon Skeet


Is it meaningful to declare a method to throw an exception and a subclass of this exception, e.g. IOException and FileNotFoundException?

Usually not - most IDEs I know of even issue warnings for such declarations. What you can and should do is to document the different exceptions thrown in Javadoc.

However, is it possible to handle both exceptions if the method throws only the most generic i.e IOException?

Yes it is, you just need to ensure that the catch blocks are in the right order, i.e. more specific first. Catch blocks are evaluated in the order they are defined, so here

try {
  ...
} catch (FileNotFoundException e) {
  ...
} catch (IOException e) {
  ...
}

if the exception thrown is a FileNotFoundException, it will be caught by the first catch block, otherwise it will fall to the second and dealt with as a general IOException. The opposite order would not work as catch (IOException e) would catch all IOExceptions including FileNotFoundException. (In fact, the latter would result in a compilation error IIRC.)

like image 4
Péter Török Avatar answered Oct 29 '22 13:10

Péter Török