Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Override method which throws exception

 
ami gu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, welcome to Javaranch
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ami gu",

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

Thank you,

Rob
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic