• Post Reply Bookmark Topic Watch Topic
  • New Topic

runtime exception  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A exception that IS-A RuntimeException is an unchecked exception.

Henry
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


Surya,
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 ..

But ,


The above Circle class will give compile time error , as stated in the first rule of overriding .

Hope this one understandable .

Satya
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;

class Excep
{


void get()
{


throw new ArrayIndexOutOfBoundsException();


}


public static void main(String...aa)
{
Excep j=new Excep();

j.get();



}}

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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote: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

Bairava, you need to read the responses you get, because Henry explained it very succinctly in the very first reply.

Is an Exception a RuntimeException?

Winston
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:import java.io.*;

class Excep
{


void get()
{


throw new ArrayIndexOutOfBoundsException();


}


public static void main(String...aa)
{
Excep j=new Excep();

j.get();



}}

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


Why don't you read the responses you get? Also, you've been told several times to use Code Tags.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!