aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NumberFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NumberFormat" Watch "NumberFormat" New topic
Author

NumberFormat

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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().


OCPJP 6, OCMJD
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Jul 13, 2011
Posts: 164

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
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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.
 
Consider Paul's rocket mass heater.
 
subject: NumberFormat