• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic