File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Exceptions." Watch "Java Exceptions." New topic
Author

Java Exceptions.

Ash Acharya
Greenhorn

Joined: Jan 09, 2009
Posts: 11
Please look at the following code:
class Animal{
public void eat() throws Exception{
System.out.println("Here\'s the exception!");
}
}

class Dog extends Animal{
public void eat(){}

public static void main(String args[]){
Animal a=new Dog();
Dog d=new Dog();
d.eat();
a.eat();

}
}


when i compile this, i get an unreported exception.. why's that so>? I know overriding in java allows the overridden method in a subclass to either not declare any of the overriding method's exceptions or declare only a subtype of the declared exceptions and not declare new or broader unchecked exceptions. why's this behavior coming up? Please explain. Thanks in advance.


regards,
Ash.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

The calling method neither catches, nor declares that it throws, the exception.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

The question seems to have nothing to do with Struts. It should be in Beginning-Java forum...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
The method a.eat throws Exception() and as the calling method, main() would have to either catch the exception or throw it again.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(In general, once a question has been answered for awhile, answering it again isn't really necessary.)
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Started to reply to the thread and got distracted with something else. Then I just submitted what I had written previously.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sorry, I mis-read the time, and thought you had replied much later than you had. My bad.
Brijesh Verma
Greenhorn

Joined: Apr 20, 2009
Posts: 19
because your using super class reference to call eat() method and at complile time compiler will call Animal method
which is throwing Exception so all Checked Exception should be in enclosed in try/catch block

Example

if you throw checked exception then you will get an error at compile time

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class InterfaceTest {

public static void main(String[] args) {


InterfaceTest a = new A();

a.add();
}

void add() throws IOException
{

}

}

class A extends InterfaceTest
{
void add(){


}

}

if you throw unchecked/runtime exception then you will not get an error at compile time

ublic class InterfaceTest {

public static void main(String[] args) {


InterfaceTest a = new A();

a.add();
}

void add() throws ArrayIndexOutOfBoundsException
{

}

}

class A extends InterfaceTest
{
void add(){


}

}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, now *that's* a late answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Exceptions.