GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding methods & Exceptions 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 "Overriding methods & Exceptions" Watch "Overriding methods & Exceptions" New topic
Author

Overriding methods & Exceptions

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The overriding method rule clearly explain that the overriding method can declare only exceptions that are either the same class or a subclass of the exception.
if that is true, how do explain the following code?

the 'RuntimeException' class is NOT a subclass of 'Error' thus making the previous code illegal, but for a strange reason that is not the case (the code compiles without issuing any error). Anyone knows why?


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Vicken Karaoghlanian:
The overriding method rule clearly explain that the overriding method can declare only exceptions that are either the same class or a subclass of the exception.
if that is true, how do explain the following code?

the 'RuntimeException' class is NOT a subclass of 'Error' thus making the previous code illegal, but for a strange reason that is not the case (the code compiles without issuing any error). Anyone knows why?


Hi Vicken,
Because they are unchecked exceptions.

JLS 8.4.4
A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
can you please explain more about checked/unchecked exceptions?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

The rules for overriding apply only to checked exceptions, you can declare any runtime exception you want. So you are correct with your statement that runtimeexception is NOT a subclass of error, but it is a runtime exception. When an error or a subclass of error is thrown, it's unchecked.
So, in conclusion, checked exceptions are all subtypes of Exception, excluding classes that extend RuntimeException.
Subtypes of Error and RuntimeExceptions are unchecked exceptions.
HTH
[ July 23, 2003: Message edited by: Andres Gonzalez ]
[ July 23, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
thanks Andres, that makes sence now...
two more questions:
1) the Exception class itself is unchecked, right?
2)is there any other checked/unchecked exception (other than the ones you've mentioned) that i should be aware of?
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
Vicken,
NO, Exception class is a CHECKED Exception.
If a method A() declares throwing an Exception then the method calling A() should check for Exception either by catching it or declaring/throwing it.
Hope this clarifies.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
i figured that earlier many thanks anywayz.
 
GeeCON Prague 2014
 
subject: Overriding methods & Exceptions