• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using try-catch in the servlet code

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic