bairava surya wrote:Runtime exception can be thrown from any method without being specified as the part of method's public interface!Help me to understand this statement with the help of program?
This is a very high label question , better if you ask more specific one .
Ok, let me answer this :
According to the JAVA overriding rule it is stated :
1)If the superclass/interface method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
2)If the superclass/interface method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
Lets say I have shape Interface :
I have a circle class that implements the shape :
The Circle class code is completely fine and it will give error at runtime ..
The above Circle class will give compile time error , as stated in the first rule of overriding .
Hope this one understandable .
posted 5 years ago
throw new ArrayIndexOutOfBoundsException();
public static void main(String...aa)
Excep j=new Excep();
This program produces the runtime time exception but when i change it as (throw new Exception) in the get method its an compiler error!provide me some reason
S Majumder wrote: . . . If the superclass/interface method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception. . . .
It might be better to say that you should not declare an Exception in an overriding method which is not declared in the overridden method. The javac tool does not check about RuntimeExceptions but that sort of thing is still poor code design. You can write all sorts of things which are poor design but they will still compile.
I would say you should never declare a RuntimeException in a throws clause, but if you throw one explicitly you should describe it in the documentation comments.