• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions in general  RSS feed

 
vipul bondugula
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an interface which contains a method which throws Exception.


A class implements "A" interface by defining the methodA().


Here, the child class "B" overrides methodA() which does not throw Exception. What is the purpose of throwing the exception in interface eventhough the implemented class didnot throw it. why the overriding method does not throw the Exception. why is throws clause optional for the sub class implementing parent methods

Thanks
Vipul Kumar.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:What is the purpose of throwing the exception in interface

To answer this, let's first look at the opposite of your code example.

If the implementation of the method in MyClass needs to have a 'throws' clause, but it is not specified in the interface, then you get a compiler error. An implementation of an interface method (or a method that overrides a method from a superclass) can only throw the same exceptions as is specified in the interface or the superclass (or a subset of those exceptions). That is because you can assign an instance of the implementing class or subclass to a variable of the interface or superclass type:


vipul bondugula wrote:why the overriding method does not throw the Exception. why is throws clause optional for the sub class implementing parent methods

The other way around, as in your code example, is no problem. The interface says the method might throw an exception, so the compiler will force you to handle it. If the implementing method really never throws the exception, then that doesn't matter.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:What is the purpose of throwing the exception in interface eventhough the implemented class didnot throw it.

None whatsoever. If no implementing class will ever throw an Exception from methodA then there is no need to declare the method in the interface as throwing an Exception.
However if any implementing class does throw an Exception then this has to be declared in the interface. Consider the following code

If the interface wasn't declared to throw an Exception how would the compiler know that the above code might throw an exception. It doesn't know that getClassThatImplementsA is returning a D object. All it knows about the returned object is that it implements A.
 
vipul bondugula
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
The other way around, as in your code example, is no problem. The interface says the method might throw an exception, so the compiler will force you to handle it. If the implementing method really never throws the exception, then that doesn't matter.


Thanks jesper de jong
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!