• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using try-catch in the servlet code

 
rammie singh
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body..
I am developing an application. To check the code standards i am running pmd which i have integrated with my IDE.

On each of the servlet i am using try catch as follows





well it means that my all case statements are in try catch block.

Problem 1st
But when i ran PMD for this file....it gives an error saying

"Avoid using catch-all blocks; except in 'main()'."

well i am not getting the meaning of this result.
does it mean that for each case i should put try-catch seperately...??
can any body guide me please.

Problem 2nd

In finally block i am using out.close() to free the resources.
if use System.out.close() ..... will it have the same effect or using out.close() is necessary.

Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Avoid using catch-all blocks; except in 'main()'."


Obviously PMD doesn't know about servlet architecture. Ignore that advice. Your arrangement will work fine, but I would add the actionSource variable to the exception logging message and think about a better way to notify the client that there was a problem.

In finally block i am using out.close() to free the resources.
if use System.out.close() ..... will it have the same effect or using out.close() is necessary.


NOT the same! System.out is shared by all the servlets and the servlet container. You should only close streams you open.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also quite common to register a common handler in the deployment descriptor and not catch anything in the servlets. If you are planning to handle exceptions using a common means, then there's no need to try/catch in every servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic