• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

doubt in throws Exception

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class sup{
void method() throws Exception{
System.out.println("sup");
}
}
public class NewMain1 extends sup{

/** Creates a new instance of NewMain1 */
public NewMain1() {
}
@Override
void method(){
System.out.println("new");
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{//#1
// TODO code application logic here
sup s = new NewMain1();
s.method();
}

}
why we should throws Exception at #1?
Overload method() doesn't throws Excpetions?

best wishes.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler sees only the reference type when choosing the method to be invoked.


The compiler thinks you are trying to invoke the superclass method which throws an exception. So it requires the main to also throw or catch the exception.

Hope this helps!
 
Storm Zcm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
Maybe JVM doesn't so clever as i think
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi As Angel...

lets first see your code...

in the first class your declared a method, which throws an Exception.
then you created a class that IS-A sub-class of the first class.
then in the sub-class, you declared an overloaded version of the method
declared in the first class.
this overloaded version does not throw any exception(As per your Calculation)

and you declared the main method as throwing an Exception
...
Now what happens ...?

At compile time, the compiler sees that an object is being referenced using a super type reference variable... and a method is being invoked on it.

so it checks if the method is present in both the classes. which is true.
and it comes to the conclusion that there is an overloaded version in the subclass.
now according to Inheritance, every class that extends from another class, inherits every variable,property that is supposed to be possessed by the super class... so the subclass unknowingly inherits the Exceptions declared by the superclass.
so.when a class inherits exceptions,it is supposed to throw them...so
we have to

1) either declare that the method (method of subclass)throws Exception.
OR
2) Enclose the invocation of the method in try/catch block.
OR
3) declare that the method throws an Exception

... Now did you understand why we declared that main throws an Exception.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sandeep atluri:
so it checks if the method is present in both the classes. which is true. and it comes to the conclusion that there is an overloaded version in the subclass.

No. The compiler only looks at the type of the reference variable. The subclass is of no interest at this point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic