This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
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?
g Peshone
Ranch Hand

Joined: Oct 20, 2008
Posts: 44
You can bang your head at the wall but do you really do that? Why would you do that?
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
I think you mean you get a compile error because you made your reference of type "sup".

sup s=new test();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Sep 14, 2008
Posts: 105
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();
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Pawan, what error do you get?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
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.
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
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

Joined: Oct 06, 2007
Posts: 338
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

Joined: Sep 14, 2008
Posts: 105
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

Joined: Oct 19, 2008
Posts: 11
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception