File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Override method which throws exception" Watch "Override method which throws exception" New topic

Override method which throws exception

ami gu

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

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:


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

Joined: Oct 27, 2005
Posts: 20274

"ami gu",

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

Thank you,


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
I agree. Here's the link:
subject: Override method which throws exception
jQuery in Action, 3rd edition