title: Spring MVC Exception Handling with @controlleradvice
body: <p>I am making a project in Spring MVC in which I need to use global exception handling for which I am using @controlleradvice and @exceptionhandler. The problem is every time I am throwing an exception it is showing the normal Tomcat exception rather than showing my customized error page. </p>
  
  <p>The code for my global controller is as follows:</p>
  
  <pre><code>package com.mphasis.insurance.controller;
  
  import java.sql.*;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  
  @ControllerAdvice
  @EnableWebMvc
  public class GlobalExceptionHandler extends ResponseEntityExceptionHandler 
  {
  
      @ExceptionHandler(value=RuntimeException.class)
      public ModelAndView handle(RuntimeException ex)
      {
  
          System.out.println("Runtime Exception Occured");
          return new ModelAndView("RuntimeException");
      }
      @ExceptionHandler(value=NullPointerException.class)
      public ModelAndView handleNullPointerException(NullPointerException ex)
      {
  
          System.out.println("Null Pointer Exception Occured");
          return new ModelAndView("NullPointerException");
      }
      @ExceptionHandler(value=ArrayIndexOutOfBoundsException.class)
      public ModelAndView handleArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException ex)
      {
  
          System.out.println("Array Index Out Of BoundsException Occured");
          return new ModelAndView("ArrayIndexOutOfoundException");
      }
      @ExceptionHandler(value=ArithmeticException.class)
      public ModelAndView handleArithmeticException(ArithmeticException ex)
      {
  
          System.out.println("Arithmetic Exception Occured");
          return new ModelAndView("ArithmeticException");
      }
      @ExceptionHandler(value=SQLException.class)
      public ModelAndView handleSQLException(SQLException ex)
      {
          System.out.println("SQL Exception Occured");
          return new ModelAndView("SQLException");
      }
      @ExceptionHandler(value=NumberFormatException.class)
      public ModelAndView handleNumberFormatException(NumberFormatException ex)
      {
          System.out.println("Number Format Exception Occured");
          return new ModelAndView("NumberFormatException");
      }
  }
  </code></pre>