Declare method to throw an exception and subclass

2020-07-11 05:58发布

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?

6条回答
可以哭但决不认输i
2楼-- · 2020-07-11 06:21

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

catch(IOException ex){
 if(ex instanceof FileNotFoundException){}
}

But this doesn't look clean, Throwing both exception looks good, even caller would come to know to that this method may throw these these exceptions, so they will handle it properly

查看更多
闹够了就滚
3楼-- · 2020-07-11 06:24

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.)

查看更多
神经病院院长
4楼-- · 2020-07-11 06:27

yes. when certain specialized exceptions can be handled correct. It is, if you handle the exceptions as follow:

try {
} catch (FileNotFoundException f) {
//Try a different file
} catch (IOException ioe) {
//Fatal, Maybe bad blocks ... Bail out... 
} catch (Exception e) {
//Something went wrong, see what it is...
}
查看更多
我命由我不由天
5楼-- · 2020-07-11 06:37

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.

查看更多
够拽才男人
6楼-- · 2020-07-11 06:38

Declaring, that the method may throw (more generic) IOException, and (more specific) FileNotFoundException is usually a good thing - it's an additional information for people using your code later. Note that you should explicitely state in the JavaDoc, under what circumstances is each of the exceptions thrown.

They will still be able to distinguish the exceptions, and handle them differently using catch constructs like this one:

try {
    yourAwesomeMethod()
} catch (FileNotFoundException ex) {
    // handle file-not-found error
} catch (IOException ex) {
    // handle other IO errors
}
查看更多
做个烂人
7楼-- · 2020-07-11 06:41

Yes, it's possible to handle both if the method only throws IOException.

The best way to answer such a question is to write a test to demonstrate it and try it out. Let the JVM tell you the answer. It'll be faster than asking here.

查看更多
登录 后发表回答