• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions thrown in interfaces and their implementations

 
Ioan Damian Sirbu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am trying to understand a situation that occurs when implementing a method in an interface and throwing a wider exception type.

Declaring an exception in SudetImpl does not compile and it's OK,because the overriding method can NOT declare wider exceptions


The weird thing comes next: when StudentImpl.speak() throws RuntimeException, the code compiles



In my opinion, there is absolutely no dfference - StudentImpl.speak() throws RuntimeException, while Student.speak() does not.
It should throw a compiler error... and yet, it does not


If someone could enlighten me regarding this, I would be really grateful.

Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because RuntimeException (and any subclass of RuntimeException) is an unchecked exception. You don't even have to declare that your method throws RuntimeException - you can even throw it from inside the method without declaring it.

See Lesson: Exceptions in Sun's Java Tutorials to learn about checked and unchecked exceptions.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not required to catch RunTimeException. Your code will compile fine even if you are not catching this exception or subclass of this exception.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuntimeException can be thrown anything because that is an unchecked exception and Unchecked Exceptions need not be declared or caught
the difference between checked and unchecked exceptions:
Checked : Compiler checks whether the exceptions thrown are caught in the program somewhere or not
Unchecked: Compiler does not check the exceptions are caught somewhere or not, means they occur at runtime , like ArrayIndexOutOfBoundsException
 
Ioan Damian Sirbu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I suspected it but I was not sure.
So basically, adding an unchecked exception in a method's declaration has absolutely no effect.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damian Sarbu wrote:So basically, adding an unchecked exception in a method's declaration has absolutely no effect.

Exactly. and you *shouldn't*
 
Ioan Damian Sirbu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expected that the compiler would give at least a warning

Thank you all for your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic