Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormatException

 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it a checked or unchecked exception
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I tell you the answer right away, it's not a good way to learn Do you know the difference between unchecked and checked exception? If you do, you should be able to answer your own question.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per my understanding it is a checked exception.

and.. as per rules of overriding..
the overridding method should not throw new or broader exceptions.
hence i am confused with the below question in k&B book excercise for chapter -5 qustion no.3..
only a & d should be correct.
but answer involves e&f also which throw new exception numberformatexception. thats my confusion.

------------
class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { // no Exceptions }
public static void main(String [] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error -
// unreported exception
}
}
------------
Which, inserted independently at // insert code here, will compile, and produce the output
b? (Choose all that apply.)
A. String doFileStuff() { return "b"; }
B. String doFileStuff() throws IOException { return "b"; }
C. String doFileStuff(int x) throws IOException { return "b"; }
D. String doFileStuff() throws FileNotFoundException { return "b"; }
E. String doFileStuff() throws NumberFormatException { return "b"; }
F. String doFileStuff() throws NumberFormatException,
FileNotFoundException { return "b"; }
Answer:
�� 3 A, D, E, and F are correct. It���s okay for an overriding method to throw the same
exceptions, narrower exceptions, or no exceptions. And it���s okay for the overriding
method to throw any runtime exceptions.
���� B is incorrect, because the overriding method is trying to throw a broader exception.
C is incorrect. This method doesn���t override, so the output is a. (Objective 2.4)
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NumberFormatException is a RuntimeException (unchecked exception) and for overriding method, you're allowed to throw any RuntimeException. Thus E and F are correct.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.i checked in API.its a runtime exception.

what i thougt is
JVM thrown == runtime exception (unchecked)
Programatically thrown = compile time(checked)

hence my understanding is wrong.

so please tell me how to differentiate between runtime and compile time exceptions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic