Why do some Java methods throw exceptions that are subclasses of another exception that they also throw?

One example is org.apache.commons.httpclient.HttpClient.executeMethod(HttpMethod method). It throws the HttpException, which is a subclass of the IOException, which it also throws. Syntactically, it would have been okay to just throw the more general (in this case, IO) exception and to omit the HttpException from the throws clause.

Does listing more specific exceptions in the throws clause of a method beside a more general one accomplish anything?

  • What's your question about - why throw or why declare throws? – Ordous Sep 30 '16 at 15:29
  • that sounds like a pedantic question but i'll answer it nonetheless: why declare a redundant throw – amphibient Sep 30 '16 at 15:31
  • 1
    The body and title are different, hence the question. As to the answer, is this one not suitable for you? – Ordous Sep 30 '16 at 15:34
  • so basically, as per that answer, it is completely redundant, correct ? – amphibient Sep 30 '16 at 15:37
  • It has no syntactic implications, yes. Just emphasis, that might have been better put into Javadoc. – Ordous Sep 30 '16 at 15:38

The usual reason for having separate exceptions is that it gives more information to the caller.

For example, IOExceptions usually refer to connectivity issues e.g. sockets closing, files not available. HttpException probably refers to a problem with the http protocol.

The practical reasons for differentiating are two-fold:

  1. you may wish to recover differently to the exceptions e.g. it could be worth retrying a broken socket but probably not if the socket isn't actually connected to an http server.
  2. you may wish to log/report the exceptions differently.

Having one a subclass of the other is useful for ergonomics. If you don't want to handle them separately you can just catch IOExceptions and handle them once. This is better than a list of catch blocks that do the same thing or, worse, catching Exception which is generally bad form as you could end up in an indeterminate state.

  • 2
    The question wasn't about why throw multiple exceptions that are subclasses of each other, but rather why would you declare it in the method signature (this was clarified in the question comments) – Ordous Sep 30 '16 at 16:14
  • 2
    The answer is relevant anyway; I upvoted. – Robert Harvey Sep 30 '16 at 16:28
  • 1
    @ordus the title of the question currently is "Why throw exception that is subclass of another exception also thrown?". If you feel that the title is wrong you could suggest an edit. – Bent Sep 30 '16 at 19:06

There is no difference for the compiler, it will not enforce anything on top of what it already does. Some people do it to emphasize that this is a particular subclass of exceptions you might want to consider. Quite a few IDE's flag it up as a warning, since (and I agree with them here) the correct place for such emphasis is the method Javadoc.

(Original content taken from here, however I didn't find a suitable duplicate on this site)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.