• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

runtime exception

 
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?
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
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
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
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.
 
Marshal
Posts: 70308
283
  • 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.
 
Slime does not pay. Always keep your tiny ad dry.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic