File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions: Handle or Declare doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions: Handle or Declare doubt" Watch "Exceptions: Handle or Declare doubt" New topic
Author

Exceptions: Handle or Declare doubt

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
For Checked Exceptions, all the methods can Duck the exception by either handling it or declaring it. Now when the exception is ducked to main() and the main just declares it the JVM will shutdown the appliction.
My doubt is... Is it ok to just declare the Exception at main() or should it be handled. In the exam if a question comes what should I answer?


If you worry you cannot work... If you work you need not worry
Arun Krishnan Nair
Greenhorn

Joined: Aug 14, 2008
Posts: 18
This question is more related to a development strategy. I guess such questions are not likely to be part of SCJP.

To answer your question, it is always better to handle exception at the software side than letting the JVM handle it.


SCJP 5 - 100%
Aiming for SCBCD 5 & SCWCD 5
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
If a code is given to identify the problem, what should be the answer?
Is it ok for main() to either declare or handle it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Its just a matter of ending the program gracefully. If you don't handle exceptions in main then sometime or the other the user of the program will have to see the not so user friendly Stack Trace....To avoid this you can declare everything in the main method in a try-catch block and then in case of any exception you can display the message of the exception to the user...

No question in SCJP will ask about this.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arun Krishnan Nair
Greenhorn

Joined: Aug 14, 2008
Posts: 18
Yes it can do either of the two ways...but better to be handled
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
K&B Self Test page 404 has this concept. I was lazy(actually busy revising) to type the entire question so I put it in words
 
Consider Paul's rocket mass heater.
 
subject: Exceptions: Handle or Declare doubt
 
Similar Threads
doubt in wait()
Method Overriding doubt
Exceptions Doubt
Overriding doubt in K&B
Overriding doubt