Enhancing Exception Handling

We can also handle exceptions in filters. Here's the doFilter method for an error handler filter:

    public void doFilter(
      ServletRequest request,
      ServletResponse response,
      FilterChain chain) throws IOException, ServletException {                
        try {
           chain.doFilter(request, response);
        } catch (Exception e) {
            PrintWriter out = response.getWriter();
            out.println(
      "sorry, you must have entered a negative number, try again");
            out.println("</body></html>");
            out.close();   
        }
    }

Here's a request:

And here's the response: