Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Java Exceptions.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9686
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question seems to have nothing to do with Struts. It should be in Beginning-Java forum...
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I mis-read the time, and thought you had replied much later than you had. My bad.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, now *that's* a late answer.
 
I've never won anything before. Not even a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic