• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Overriding Exception error

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



compile error:----unreported exception java.lang.Exception; must be caught or declared to be thrown
But why.i have already declared Exception ? please help me to figure it out.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:



compile error:----unreported exception java.lang.Exception; must be caught or declared to be thrown
But why.i have already declared Exception ? please help me to figure it out.



where have you declared??
I cant see it?

Cheers
Prashanth
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem has nothing to do with inheritance. You need your main method to declare the checked exception.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,

when a method declares to throw a checked exception, it must be declared or handled every time you use this method.

Try try.


Yours,
Bu
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shiva,
Either changing the signature of the main method by throwing an exception broader or equal to Exeception

or, by declaring a catch block around the called method with a argument broader enough to catch the exception thown by the method.

will solve this compilation error.
All the checked exception must be catched or thrown to the caller.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Place a try-catch block in main method surrounding myQ.priya().

or declare your main method to throw Exception as


public static void main( String args[] ) throws Exception{
Q1 myQ=new Q1();
myQ.priya();
}

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the time.One more doubt on this.

The above same program,but in the main method i changed like the below.


Q1 myQ=new Q();
System.out.println("class"+myQ.getClass());
myQ.priya();



//priya() method should take runtimeBinding and displays xxxx.
myQ.getclass also displaying class Q.then why do we need to declare Exception in mainmethod.I thought we needed to declare EOFException since Q class priya() method is executing.
i am looking for the rules behind this .please help me again.
[ November 09, 2006: Message edited by: Shiva Mohan ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,

Please remember,

"Method calls at compile time are always resolved by the type of the reference.
Only at runtime the actual overridden method is called, depending on the type of the object that the reference points to."

Here the reference myQ defined in main is of type Q1. At compile time it resolves to the method that is defined in Q1 class. Since this throws Exception. You have to handle this, not the EOFException thrown by the overridden method in Q class.

- Ramu
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiva you can verify this by changing your main method as shown

throws the exception
Q1.java:18: unreported exception java.lang.Exception; must be caught or declared to be thrown
myQ.priya();
^
1 error

Though we are calling the subclass method but it is the super class reference which is pretending that his method is going to be called,hence the compilation error of not being catched the java.lang.Exception is thrown rather than the java.lang.EOFException.

Only at runtime the actual overridden method is called, depending on the type of the object that the reference points to.


But at runtime the subclass version is going to be called..

This is what we called is runtime polimorphism.
[ November 09, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Have you no shame? Have you no decency? Have you no tiny ad?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic