This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding with Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding with Exception" Watch "overriding with Exception" New topic
Author

overriding with Exception

Deepak Bajoria
Ranch Hand

Joined: Mar 07, 2008
Posts: 35
Can someone please clarify the rules of overiding with Exception.

I know that the overriding method can dclare same or a subtype of exception to wat was declared in overridden method.

But I didnt get this:
The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.
-K&B, pg.102

Can someone provide a rough code sample wat this mean.

Thanks in advance


SCJP 5 - 93%
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342


SCJP 5 (98%) - SCBCD 5 (98%)
vaibhav mishra
Ranch Hand

Joined: Jun 18, 2008
Posts: 168
Ralph ,can you clarify more


SCJP
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
What exactly ?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Basically, what it is saying is that when you override a method, you can't start throwing any new checked exceptions that the method in the parent class didn't already throw.

Look at the following SCJP Mock Exam Quiz Questions on Exceptions:


Given the following code, which of the following methods could be defined in the SunCertified class without causing a compile error?

public class Certified {
public void getCertified()throws GreatGrandChildException { }
}

class SunCertified extends Certified {}


class ChildException extends Exception{}
class GrandChildException extends ChildException{}
class GreatGrandChildException extends GrandChildException {}


a) public void getCertified() throws Exception { }
b) public void getCertified() throws ChildException { }
c) public void getCertified() throws GrandChildException { }
d) public void getCertified() throws GreatGrandChildException { }



Option d) is correct.

An important rule in Java says that when you override a method in a subclass, you cannot throw any checked Exceptions other than those declared in the method you are overriding. So, since the parent, Certified class does not indicate any checked exceptions in the getCertified() method's throws clause, the overriding method in the SunCertified class is not allowed to throw any new Exceptions either.

Trying to override a method in a subclass, and subsequently throwing a new type of exception in the overriding method, will generate a compiler error such as this one:

C:\_jdk1.6\bin>javac Certified.java
Certified.java:9: getCertified() in SunCertified cannot override getCertified()
in Certified; overridden method does not throw java.lang.Exception
public void getCertified() throws Exception { }
^
1 error


However, the rules for a checked Exception are not the same for Error objects and RuntimeException objects. An overriding subclass can throw any number of Error objects or RuntimeException objects from its throws clause that it likes, regardless of what the throws clause was in the method of the parent class that the child is overriding.

Take a look at this other mock exam test question for Exception on the SCJP exam:


Given the following code, which of the following methods could be defined in the SunCertified class without causing a compile error?

public class Certified {
public void getCertified() throws GreatGrandChildException { }
}
class SunCertified extends Certified {}

class ChildException extends Exception{}
class GrandChildException extends ChildException{}
class GreatGrandChildException extends GrandChildException {}

a) public void getCertified() { }
b) public void getCertified() throws ClassCastException { }
c) public void getCertified() throws ChildException, IllegalStateException { }
d) public void getCertified() throws GrandChildException, IllegalStateException { }
e) public void getCertified() throws GreatGrandChildException, NoClassDefFoundError { }


Options a) b) and e) are correct.

When you override a method that has a throws clause, it is completely valid for your overridden implementation of that method not to have a throws clause at all, making option a) correct.

Furthermore, an overridding method is allowed to throw any number of unchecked, RuntimeException or Error objects, regardless of whether the RuntimeException or Error object was thrown in the inherited method that is being overridden, making option b) completely valid.

Options c) and d) will not compile, as the overriding method tries to throw exceptions that are less specific than the one thrown by the getCertified() method in the parent class. ChildException and GrandChildException do not share an is-a relationship with the GreatGrandChildException, and as a result, throwing them in the overridding method generates a compile error.

Option e) will indeed compile, as both the overridding method and the overridden method both throw the exact same checked exception, the GreatGrandChildException. The overridden method also throws the NoClassDefFoundError, but overriding methods can throw any number of Errors that are not explicilty defined in the method being overridden, so this does not cause a problem when using option e).

I'm working on a bunch of mock SCJP6 exam questions on my blog. They're a bit messy right now, but they're still pretty good.

-Cameron McKenzie
[ July 20, 2008: Message edited by: Cameron Wallace McKenzie ]
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Basically, what it is saying is that when you override a method, you can't start throwing any new unchecked exceptions that the method in the parent class didn't already throw.
The bold word has to be "checked" !!!

Unchecked exceptions can be startet in the overriding method as much as you like, absolutely independent of the unchecked exceptions thrown by the overriden method in base class (see my code example above).
[ July 20, 2008: Message edited by: Ralph Jaus ]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

What I said is less important than what I mean.

Typing too quickly, and not paying attention.

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
Java 6 API JavaDoc

-Cameron McKenzie
[ July 20, 2008: Message edited by: Cameron Wallace McKenzie ]
 
wood burning stoves
 
subject: overriding with Exception