• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Exceptions.

 
Ash Acharya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question seems to have nothing to do with Struts. It should be in Beginning-Java forum...
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(In general, once a question has been answered for awhile, answering it again isn't really necessary.)
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I mis-read the time, and thought you had replied much later than you had. My bad.
 
Brijesh Verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now *that's* a late answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic