0

i have this program in java

public class PolyTest
{
    public static void main(String... arg)
    {
        Animal a = new Animal();
        Horse b = new Horse();
        Animal c = new Horse();

        a.eat();
        b.eat();
        c.eat();
    }
}

class Animal
{
    public void eat() throws IndexOutOfBoundsException
    {
        System.out.println("Animal Eating");
    }
}
class Horse extends Animal
{
    @Override
    public void eat()
    {
        System.out.println("Horse Eating");
    }
}

now, surprisingly it works without any error in-spite of no try catch block or thrown clause in main method.
1. why a.eat() is not giving any error in main method ?
2. when I change IndexOutOfBoundsException to simply Exception, it is a compile time error. why ?

read something about this in kathy sierra's SCJP exam guide, but couldn't understand the concept here.

Shashi
  • 746
  • 10
  • 39
  • IndexOutOfBoundsException is a RuntimeException, so it doesn't need special treatment. – wxyz Dec 22 '13 at 17:39
  • IndexOutOfBoundsException is a RuntimeException. See http://stackoverflow.com/questions/2190161/difference-between-java-lang-runtimeexception-and-java-lang-exception. – jarmod Dec 22 '13 at 17:39
  • possible duplicate of [No compilation error on Runtime Exceptions. why?](http://stackoverflow.com/questions/6150609/no-compilation-error-on-runtime-exceptions-why) – Mat Dec 22 '13 at 17:39

6 Answers6

3

You needn't to catch IndexOutOfBoundsException , it's RuntimeException.

Examine types of Exceptions

alex2410
  • 10,904
  • 3
  • 25
  • 41
3

The main confusion here is in distinguishing between actually throwing an exception and just declaring it. The confusion is brought about by Java's unfortunate "feature" of checked exceptions because calling a method which just declares to throw a checked exception will make you do something about it regardless of whether that method may actually ever throw the exception.

On the other hand, the compiler will never make you do anything about unchecked exceptions, whether they are actually thrown or not. A method may declare to throw an unchecked exception, but the compiler basically ignores that declaration. It may serve for documentation purposes, but is generally not written at all.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Not all exceptions need to be handled by programmer. Some of them simply are supposed to crush app because they represent problem with code rather then with input. Such exceptions are RuntimeExceptions. Most famous is NullPointerException which indicates that some variables don't hold references to objects, but still are used to invoke methods of that object.

Now take a look at IndexOutOfBoundsException documentation and you will see that it extends RuntimeException which means you don't need to (but you can) catch it. That is why your code compiles without problems.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

IndexOutOfBoundsException is an unchecked RuntimeException which is why you are not required to handle it.

Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18
0
  1. Why do you think it should give an error?
  2. IndexOutOfBoundsException is an unchecked exception therefore you don't have to declare in a throws list of the method main, Exception is a checked exception which gives a compile time error because you don't declare it in throws section in the main method and you don't use a try/catch block.
Ahmed
  • 2,176
  • 5
  • 26
  • 40
A4L
  • 17,353
  • 6
  • 49
  • 70
0

You have to firmly understand why you should not catch RuntimeException. Because thise type of exception indicates your programming error. Normally this type of exception should not appear. For example if you have program where the user enter the index of something in your array, you should firstly check the data user has entered and only then pass this data to the program. If you did not checked and user entered -1 you will get runtime exception that will indicate your programming error.

Oleksandr Papchenko
  • 2,071
  • 21
  • 30