Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions: Handle or Declare doubt

 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Arun Krishnan Nair
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Krishnan Nair
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it can do either of the two ways...but better to be handled
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic