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– Ordous2016年09月30日 15:29:22 +00:00Commented Sep 30, 2016 at 15:29
-
that sounds like a pedantic question but i'll answer it nonetheless: why declare a redundant throwamphibient– amphibient2016年09月30日 15:31:15 +00:00Commented Sep 30, 2016 at 15:31
-
1The body and title are different, hence the question. As to the answer, is this one not suitable for you?Ordous– Ordous2016年09月30日 15:34:51 +00:00Commented Sep 30, 2016 at 15:34
-
so basically, as per that answer, it is completely redundant, correct ?amphibient– amphibient2016年09月30日 15:37:59 +00:00Commented Sep 30, 2016 at 15:37
-
It has no syntactic implications, yes. Just emphasis, that might have been better put into Javadoc.Ordous– Ordous2016年09月30日 15:38:55 +00:00Commented Sep 30, 2016 at 15:38
2 Answers 2
The usual reason for having separate exceptions is that it gives more information to the caller.
For example, IOException
s 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:
- 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.
- 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 IOException
s 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.
-
2The 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– Ordous2016年09月30日 16:14:30 +00:00Commented Sep 30, 2016 at 16:14
-
2The answer is relevant anyway; I upvoted.Robert Harvey– Robert Harvey2016年09月30日 16:28:10 +00:00Commented Sep 30, 2016 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– Bent2016年09月30日 19:06:24 +00:00Commented Sep 30, 2016 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)