Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NumberFormat

 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 6 Question 13 page 538.



Statement G is included in the correct answer:
G. The invocation of parse() must be placed within a try/catch block.


Can you please explain why this is so and why no try/catch is required around "nf.setMaximumFractionDigits(5);" ? Surely the risk of an exception is as high with this statement "nf.setMaximumFractionDigits(5);" as it is with the parse().
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with risk of an exception. The parse() method can throw a ParseException - a checked exception - which always needs to be caught or declared. The setMaximumFractionDigits() method does not.
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:It has nothing to do with risk of an exception. The parse() method can throw a ParseException - a checked exception - which always needs to be caught or declared. The setMaximumFractionDigits() method does not.


Thanks Setphan. How can you tell if an exception is checked or not? OR how can you tell if a method needs to be caught or declared?
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen Iris wrote:
Thanks Setphan. How can you tell if an exception is checked or not? OR how can you tell if a method needs to be caught or declared?


An exception is an unchecked exception if it IS-A RuntimeException. You can look it up -- meaning whether the method throws an exception and/or whether an exception is checked or not -- in the JavaDoc. As for "if a method needs to be caught or declared", that is your decision. You are the coder for your application. And it is your choice to handle the exception at the method, or pass the responsibility to the caller.

Henry
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding to exception handling, I saw some code like this the practice exam book:

try{
t.start() ; //where t is a thread
}

catch (Exception e){}




As I checked with the API, Thread's start() method does not throw any exception at all. But this code still compiles. However, if we change the Exception to be IOException , let's say, the code won't compile because start() does not throw any IOException.

I don't see why the above code still compiles. How about replacing Exception by Throwable?
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unchecked exceptions may arise anywhere, so you may catch them anywhere. This includes catching Exception, Throwable or Error.

You may only explicitly catch checked exceptions if they can be thrown inside the try clause.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic