aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions thrown in interfaces and their implementations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions thrown in interfaces and their implementations" Watch "Exceptions thrown in interfaces and their implementations" New topic
Author

Exceptions thrown in interfaces and their implementations

Ioan Damian Sirbu
Greenhorn

Joined: Dec 22, 2008
Posts: 18
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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

Joined: Mar 07, 2010
Posts: 438

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

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Ioan Damian Sirbu
Greenhorn

Joined: Dec 22, 2008
Posts: 18
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 22, 2008
Posts: 18
I would expected that the compiler would give at least a warning

Thank you all for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions thrown in interfaces and their implementations
 
Similar Threads
Exceptions and overridden methods
deadly diamond of death syndrome :)
RMI Project
java.rmi. NotBoundException: Student1001?
java.security AccessControlException : access denied?