Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop printing exception stack trace on Console?

Tags:

I wrote a servlet to handle the exceptions occurring in my web app and mapped them in web.xml

    <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/exceptionHandler</location>
    </error-page>

Here is what I have done in the Exception Handling servlet service method:

@Override
    protected void service(HttpServletRequest req, HttpServletResponse arg1)
            throws ServletException, IOException {
         Object attribute = req.getAttribute("javax.servlet.error.exception");
         if(attribute instanceof  SocketException){
            // don't do anything 
         }else{
          super.service(req, arg1);
         }
    }.

Problem:

The above approach is not working and the stack trace is printing to the console. This occurs when the user requests something and then closes their browser.

Question:

How do I stop printing the stacktrace to the JBoss console whenever a SocketException occurs?

Reason for doing this:

I want to avoid seeing all of the log's SocketExceptions at the end of th day because I can't do anything with that information.

like image 614
Suresh Atta Avatar asked Sep 17 '13 12:09

Suresh Atta


People also ask

What are the different ways to print exception message on console?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

What is exception stack trace?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

Should we print stack trace?

As someone who has to troubleshoot regularly, I would never omit printing a stacktrace when something has gone wrong. Yes, don't show it to the user, but yes, dump it to an error log.


2 Answers

Here is what I done so war as work around.

Added one filter and hijack all the request and response.Catch the exception and check the type.

/**
 * Hijacks all the http request and response here.
 * Catch the SocketException and do not print 
 * If other exceptions print to console
 * date : 9-18-2013
 * 
 * @author Suresh Atta
 *
 */
public class ExceptionHandler implements Filter {

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        try{
        arg2.doFilter(arg0, arg1);
        }catch(SocketException e ){
           // Please don't print this to log.    
        }
    }


}

And in web.xml ,filter mapping

<filter>
        <filter-name>ExceptionHandler</filter-name>
        <filter-class>com.nextenders.server.ExceptionHandler</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ExceptionHandler</filter-name>
        <dispatcher>  REQUEST   </dispatcher>
        <url-pattern> /*</url-pattern>
    </filter-mapping>  

I'm not marking this as answer,since I'm not sure this is a standard way or not.Just a work around.

like image 85
Suresh Atta Avatar answered Oct 09 '22 00:10

Suresh Atta


Instead of adding java.lang.Exception in your web.xml why won't you just try to add socket exception in web.xml itself like below

<error-page>
  <exception-type>java.net.SocketException</exception-type>
  <location>/exceptionHandler</location>
</error-page>

and just do nothing in your servlet Or instead add a empty jsp file like

<error-page>
  <exception-type>java.net.SocketException</exception-type>
  <location>/error.jsp</location>
</error-page>
like image 44
Prakash Avatar answered Oct 08 '22 23:10

Prakash