This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and Exceptions" Watch "Overriding and Exceptions" New topic
Author

Overriding and Exceptions

Nils Widmer
Greenhorn

Joined: Aug 17, 2001
Posts: 13
Given Following Code, why is it not allowed for MyMethod() to declare IOException in MyExp2 class( question 9 from http://www.javacaps.com/scjp_mockexams1.html )???
Overriding a method that doesnt throw Exception wont have overriden method which does throw Exception?
Do Overriden methods have some scope rule for throwing exceptions as in they can only throw exceptions that are already thrown in the overridden method of subclass??


<CODE>
import java.io.*;
class MyExp {
void MyMethod() throws IOException, EOFException {
//............//
}
}
class MyExp1 extends MyExp {
void MyMethod() {
//..........//
}
}
public class MyExp2 extends MyExp1 {
void MyMethod() throws IOException {
//.........//
}
}
</CODE>

Can an overloaded method throw an exception?
like:
<CODE>
class Shit
{
protected void m_DoShit()
{}
protected void m_DoShit(int i)
throws MyException
{}/*assume MyException is derived from IOException*/
}
class BullShit extends Shit
{
public void m_DoShit(int i)
throws IOException
{}
}
</CODE>

------------------
ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
Nils,
for the first part of ur query, i think this is the solution:
As class MyExp2 extends class MyExp1, and also overrides the MyMethod() method of MyExp1( which is its immediate superclass). As the MyMethod() of superclass(MyExp) doesn't throw any Exception so the overriding method in its subclass can't throw any other(Except RunTimeException) exception. In short MyMethod() of MyExp2 should be defined in the context of its immediate superclass i.e. MyExp1.
hope it should be o.kay.
ashok.
Nils Widmer
Greenhorn

Joined: Aug 17, 2001
Posts: 13
yups, thanks Ashok, that is clear now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding and Exceptions
 
Similar Threads
JavaCaps mock 1 Q:9
javacaps mock question
Exception handling throw and throws
Exception doubt
exception propagation