• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Role of Exceptions in overriding a method

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please some one explain me about the imporatnce of exceptions while overriding a method.Advanced Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't fully get the question, but I suppose you want to know what the contract of exceptions is when overriding a method.

When overriding a method that throws an exception, the overriding method may either throw no exception, or any exception that is the same or a subtype of the original declared exception ...
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Iam using Java 1.6 version.I have one overridden method and one overriding method as shown below.

Overridden method : public void div(int a,int b)throws ArrayIndexOutOfBoundsException
{
System.out.println("Hai this is the super class div method");
}
Overriding method : public void div(int a,int b)throws IndexOutOfBoundsException
{
System.out.println("Hai this is the sub class div method");
}

If you can observe Iam throwing the super class exceptio type in the overridding method(IndexOutOfBoundsException is super class of ArrayIndexOutOfBoundsException) , Still the code is getting compiled and ran.

Iam totally confused.Please help me out.Some one Give me full details.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anwar Hussain wrote:
Iam throwing the super class exception type in the overridding method(IndexOutOfBoundsException is super class of ArrayIndexOutOfBoundsException) , Still the code is getting compiled and ran.



that is because of you are throwing the RuntimeException
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You mean to say the rules will not apply to Exceptions of type RuntimeException as compiler will not care about these exceptions.
And one more thing what if the overriden method is throwing multiple exceptions???
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anwar,

Use [code] tag when you post code with your question.......

Anwar Read this Use Code Tag
 
Anwar Hussain
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

seetharaman venkatasamy wrote:

Anwar Hussain wrote:
Iam throwing the super class exception type in the overridding method(IndexOutOfBoundsException is super class of ArrayIndexOutOfBoundsException) , Still the code is getting compiled and ran.



that is because of you are throwing the RuntimeException




Hi Swamy,

Thanks alot.Everythin is clear now. Cheers.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anwar Hussain wrote:
Hi Swamy,

Thanks alot.Everythin is clear now. Cheers.



you are welcome
 
Are you okay? You look a little big. Maybe this tiny ad will help:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic