This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Override method which throws exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Override method which throws exception" Watch "Override method which throws exception" New topic
Author

Override method which throws exception

ami gu
Greenhorn

Joined: Jun 06, 2008
Posts: 2
Hello, I am studying Exception in java now. Here is a program that I cannot figure it out.

When you override a method which throws an exception in the superclass, you could choose to declare the same exception, or narrower exception, or no exception, or other runtime exception in the subclass, right? So I made the following program:



which I thought Exception is the superclass of NumberFormatException, so it should compile fine. But it did not. If I change the Exception into Runtime Exception or any of its subclass, then it compiles. Could anyone let me know what is happening here, please?
Thank you very much
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Hi,

Compiling your code, apparently your problem is not overriding the getInt() method. The line I get an error is:



where the compiler complains that the type Exception is not being handled. That happens because you are using a reference to the Utils type, which could be any kind of Utils - not only Ping. Therefore the exception type is checked against what Utils defines (Exception) rather than what Ping defines (NumberFormatException).

So, you should do:



OR:



depending on whether or not you want to keep the polymorphism in your code.
[ June 06, 2008: Message edited by: Rodrigo Tomita ]
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
By the way, welcome to Javaranch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

"ami gu",

Can you please read your private messages regarding an important question?

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Also, just to put in my two cents, the compiler looks at and sees "u" as an object of type Utils, and from there it assumes you are calling the version of the getInt() method from the Utils class, which throws Exception. You MUST be able to catch that exception, because although in your code example you happen to only call the subclass version, there is a chance that you might call the Utils version, in which case your program could crash. Another thing to mention is that when your methods throw a RuntimeException, for example NumberFormatException, you don't need to catch them (you can, but you don't need to). So if your Utils version of getInt() didn't throw any exceptions, you would not be required to handle any exceptions (unless you wanted to). You may (or may not) want to quickly research checked exceptions versus unchecked (or runtime) exceptions.


Studying for SCJP 6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Override method which throws exception
 
Similar Threads
Overriden method throwing Exception.
Question on K& B chapter 5.
Overridden method and exceptions
Exception handling problem
chapter 5,question-8,(k&b)