• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is exception handling not required in this scenario?  RSS feed

 
John Drulo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following interface:

Which is implemented by the following class:


Although meth() is a method that throws an exception, the caller of the method meth() is not having to handle or declare the exception and yet the program runs successfully. Why is this the case? Does it not violate the rule that whenever you call a method that throws an exception, you need to catch the exception or declare that you throw the exception yourself?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code listings, please UseCodeTags (←click on that link to learn how, it's really easy)
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP can't edit their post once there's been a reply, so I added the code tags for you this time. Looks a lot better, don't you think?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not getting a compile-time error because your implementation of meth() does not declare the exception. If you add the throws clause in the implementation, you'll get the compiler error you were expecting. The throws clause is not considered part of the method signature, BTW, so apples.meth is still considered an implementation of BadInterface.meth even without the throws clause.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of declaring throws Exception. you should avoid plain simple Exception as far as possible. In the case of such an interface however it may be unavoidable. So don't call it BadInterface because there is nothing bad about it.

If you write throws Exception people should interpret that as meaning you do not know whether the method will actually throw an Exception or not. Not only that, you do not know what sort of Exception it might throw. So the only way you can provide for all implementations with and without the potential to throw Exceptions is to make a non‑specific catch‑all declaration. An overriding or implementing method must not throw any exceptions other than those which are subclasses of those already declared. [Remember the javac tool doesn't check unchecked exceptions.] So, if you declare
throws XYZException, PQRException, ABCException
… those are already included in throws Exception. So that is all right: you can throw any sort of Exception.

Nowhere does it say that you have to throw any Exception. Bruce Eckel (Thinking in Java 4th edition) says that the one thing you can do with Exceptions to the javac tool is lie about them. You can declare an Exception and not throw it, thereby permitting overriding methods to throw exceptions, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!