Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

NumberFormatException

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it a checked or unchecked exception
 
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
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic