File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes using try-catch in the servlet code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "using try-catch in the servlet code" Watch "using try-catch in the servlet code" New topic
Author

using try-catch in the servlet code

rammie singh
Ranch Hand

Joined: Mar 26, 2009
Posts: 116
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

Joined: Mar 22, 2000
Posts: 12809
    
    5
"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

Joined: Jan 10, 2002
Posts: 61451
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: using try-catch in the servlet code