• 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:

Exception

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the following programme showing run time error??
class sup
{
void method() throws Exception{}
}
class test extends sup{
public static void main(String[] args)
{
sup s=new test();
s.method();
}
void method()
{
}
}
As in overridden method, we can either throw less or no any exception in accordance with base class, then what the heck wrong in this?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can bang your head at the wall but do you really do that? Why would you do that?
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you mean you get a compile error because you made your reference of type "sup".

sup s=new test();
 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by g Peshone:
You can bang your head at the wall but do you really do that? Why would you do that?



Probably for forgetting the code tags and the indentation.

Does it show a run time error? I can't even get it to compile. Try putting the @Override annotation before "void method" in your test class and see what happens.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Campbell:
I think you mean you get a compile error because you made your reference of type "sup".

sup s=new test();



You're right. I also wanna add what's the reason to have an exception when the statement is like test s=new test();
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pawan, what error do you get?
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Pawan, what error do you get?



He would of gotten something like:

unreported exception java.lang.Exception; must be caught or declared to be thrown s.method(); ^

It is because his new Test() is being assigned to a Sup reference type and the compiler knows that the Sup class's method throws an exception that must be caught.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Pawan

Originally posted by Pawan Arora:
You're right. I also wanna add what's the reason to have an exception when the statement is like test s=new test();



As Ilja says, without knowing the error its hard to answer your question.

A "small" stylistic point: Although class names do not HAVE to be capitalized, it is a standard coding convention. About the only time you will see this violated is on tests, where sneaky people like the otherwise admirable KS & BB try to trip you up.

Where this becomes relevant to your quoted additional question: After changing the type of s to test, if you are getting a run-time java.lang.NoClassDefFoundError or something similar & your classpath looks ok it COULD be that your file is named Test.java. Java is case sensitive. Just a wiiiild guess.

Here is a link I have found useful on coding style. Some people, like my first Project Manager, will jump all over you & make you want to cry if you don't follow approved style, so I learned real quick it's good to do it from the get-go; i.e. Its easier to just tuck in your shirt than listen to your Mom nag you about it.

www.javaranch.com/style.jsp

Ken "Yes That's REALLY my last name" Kisser

As my Otaku Girl would tell you: "Substance is important, but it's style that gets you past first base."
[ October 24, 2008: Message edited by: Ken Kisser ]
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pawan Arora:


You're right. I also wanna add what's the reason to have an exception when the statement is like test s=new test();


It is because the compiler knows type sup throws an exception and you've assigned your test object to a sup type. If you put your in a try-catch it will execute the overriden test method at run time
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I understood the reason to have a compilation error, but what's the reason to have an exception to be thrown at runtime in a statement test a=test();
Because in overriden method, we can either throw less or no any exception; then why Jvm is so dying to catch the exception of superclass anyhow?
[ October 25, 2008: Message edited by: Pawan Arora ]
 
Ken Kisser
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pawan

Correct me if am wrong but it is my understanding that you changed the code in the program and are getting a different error, a run-time error this time, & would like to know the cause of this new error. I suggest that you post this new version of the code, formatted to Java Ranch standards, along with the exact wording of the new error message and I will do my best to answer it. Unless one of the 2 Campbells beat me to the draw. They ARE faster. And better shots. In fact, they're "Souper."

Otherwise, can't speak for others, but as for me, feels like I'm just shooting in the dark, spitting (or whatever) in the wind, whistling down a well, asking Otaku Girl to do my laundry: in other words wasting your time, my time, & anybody masochistic enough to read my posts

kk
[ October 26, 2008: Message edited by: Ken Kisser ]
 
pie. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic