File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions and overriding 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 and overriding" Watch "Exceptions and overriding" New topic
Author

Exceptions and overriding

Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi javers.
I have found that question:

and the answer is "method1 in class NewBase does not need to specify any exceptions", but as I know the overriding method must not thow new or broader checked exception that were declared by the overriden method, but at least it have to declare (or throw) the same exception or it's subclass?
So in the above code we have to declare at least IOException or it's subclass, haven't it?
Thanks in advance.


SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you check out the JLS, §8.4.4 Method Throws, you'll see this:

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.
More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its superclasses must occur in the throws clause of m; otherwise, a compile-time error occurs.

To break that down, an overriding method can throw any subset of checked exceptions declared by the overridden method. That subset can include the entire set (meaning that the overriding method can throw all of the exceptions the overridden method can throw) and it can also include the empty set (meaning that the overriding method throws no expceptions whatsoever).
The key is that the overriding method can not throw a checked exception that the overridden method does not declare.
I hope that helps,
Corey


SCJP Tipline, etc.
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
Lets look at a reason behind this design:

In case 1, the compiler knows that method1 might throw IOException
In case 2, at runtime method1 (the overriding version in NewBase) doesn't throw IOException, however at compile time the decision has to be made based on the reference type, ie Base's method1 signature.
So, if the runtime class is a subclass of the parent, and the subclass's overriding method was allowed to throw newer or broader exceptions then common exception handling based on the parent reference's method signature would not be possible. Case 3 illustrates this scenario.
The crucial point is that using a base class reference and providing the runtime via any subclass is a powerful paradigm and would break if this exception handling rule wasn't enforced.
- ortimuS
[ April 06, 2004: Message edited by: ortimus tilap ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions and overriding
 
Similar Threads
Overriding and Exception
exception
Regarding Overiding
can anyone explain me this?
exception problem:looking at following two code fragments!