Declare a method to throw an exception and subclass this exception

Is it worth declaring a method to throw an exception and a subclass of that exception like IOException and FileNotFoundException?

I assume it is used to handle both exceptions using the caller method differently. However, is it possible to handle both exceptions if the method has selected only the most general ie IOException?

+6
source share
6 answers

However, is it possible to handle both exceptions if the method throws only the most common ie IOException?

That's right. You can still catch them separately:

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

Thus, it does not matter what the caller can do if the method declares both - it is redundant. However, it may highlight exceptions that you might want to consider. This can be done better in Javadok than just throwing shots.

+7
source

Is it worth declaring a method to throw an exception and subclass that exception, for example. IOException and FileNotFoundException?

Usually not - most IDEs that I know even warn of problems for such ads. What you can and should do is document the various exceptions thrown at Javadoc.

However, is it possible to handle both exceptions if the method throws only the most common ie IOException?

Yes, you just need to make sure that the catch blocks are in the correct order, i.e. more specific in the first place. The catch blocks are evaluated in the order in which they are determined, so here

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

if the exception FileNotFoundException is a FileNotFoundException , it will be caught by the first catch , otherwise it will drop to the second and will be considered as a general IOException . The opposite order does not work, since catch (IOException e) will catch all IOException , including FileNotFoundException . (In fact, the latter will result in an IIRC compilation error.)

+3
source

However, is it possible to handle both exceptions if the method throws only the most general ie> IOException?

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

But it does not look clean. Throwing both exceptions looks good, even the caller learns that this method can throw these exceptions, so they will handle it correctly

0
source

Yes, it is possible to handle both methods if the method throws only an IOException.

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

0
source

Yes. when certain specialized exceptions can be handled correctly. This is if you handle exceptions as described below:

 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... } 
0
source

Declaring that a method can throw a (more general) IOException and a (more specific) FileNotFoundException is generally good - this is additional information for people using your code later. Note that you must explicitly state in the JavaDoc under what circumstances each of the exceptions occurs.

They will still be able to distinguish exceptions and handle them differently with catch constructions like this:

 try { yourAwesomeMethod() } catch (FileNotFoundException ex) { // handle file-not-found error } catch (IOException ex) { // handle other IO errors } 
0
source

Source: https://habr.com/ru/post/918545/


All Articles