Learn together SpringMVC of exception handling

In the system development process, exception handling is inevitable, if an exception not handled properly, will result in a poor user experience, this paper explains SpringMVC in the development process, knowledge point exception handling, only to learn to share, such as inadequate, please correct me.

Outline

In SpringMvc, the exception handling There are several ways, this paper mainly on two scenarios:

  1. Abnormal to capture received and processed by @ExceptionHandler.
  2. The process returns to customize content by @ResponseStatus status code annotations.

By @ExceptionHandler Procedure

1. a custom exception class that inherits from class Exception

As follows: @ExceptionHandler marked on the method, this method can be used to represent handling exceptions, if multiple exceptions need to be captured, then separated by commas.

If you need to catch exceptions other classes, we need to increase @ControllerAdvice annotation on the class.

 1 package com.hex.third;
 2 
 3 import org.springframework.web.bind.annotation.ControllerAdvice;
 4 import org.springframework.web.bind.annotation.ExceptionHandler;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 /**
 8  * 自定义异常
 9  * @author Administrator
10  *
11  */
12 
13 @ControllerAdvice
14 public class MyException extends Exception {
15     
16     /**
17      * This method can capture this class ArithmeticException thrown an exception that can support multiple anomalies
 18       * @param as ex to be captured, this method must have only one argument, if there are other types of parameters, it will error
 19       * @return 
20 is       * / 
21 is      @ExceptionHandler ({an ArithmeticException. class , An ArrayIndexOutOfBoundsException. class , MyArrayOutofBoundsException. class })
 22 is      public ModelAndView handlerException (Exception ex) {
 23 is          // the background information output ex 
24          System.out.println (ex.getMessage ( ));
 25          // error information displayed in the foreground 
26 is          ModelAndView MAV = new new ModelAndView ();
27         mav.setViewName("error");
28         mav.addObject("exce", ex);
29         return mav;
30     }
31 }

2. Given a method, a math exception is thrown, you can capture and display an error page

 1 package com.hex.third;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.ControllerAdvice;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.servlet.ModelAndView;
 7 
 8 @Controller
 9 public class Exce2Controller {
10 
11     /**
12      * 抛出一个异常
13      * @return
14      */
15     @RequestMapping("/helloexce2")
16     public ModelAndView HelloExce2(){
17         try{
18             int i=1/0 ;
19         }catch(ArithmeticException ex){
20             throw ex;
21         }
22         ModelAndView mav=new ModelAndView();
23         mav.addObject("helloexce","hello exception");
24         mav.setViewName("success");
25         return mav;
26     }
27 }

3. Run the test

Output error page, as follows:

By @ResponseStatus, returns an error message, the steps

1. a custom exception class, and adds annotated @ResponseStatus

As follows: value shows a state code, is an enumerated type, reason display status information

. 1  Package com.hex.third;
 2  
. 3  Import org.springframework.http.HttpStatus;
 . 4  Import org.springframework.web.bind.annotation.ResponseStatus;
 . 5  
. 6  / ** 
. 7  * custom exception class
 . 8  * @author Administrator
 . 9  * @ResponseStatus can be expressed either in front of the class, the table may identify earlier method
 10  *
 . 11   * / 
12 is @ResponseStatus (value = HttpStatus.FORBIDDEN, reason = "Forbidden page 22222" )
 13 is  public  class MyArrayOutofBoundsException the extends Exception {
 14  
15 }

2. Define a method Thrown

As follows:

 1 /**
 2      * 测试第三个异常
 3      * @return
 4      * @throws MyArrayOutofBoundsException
 5      */
 6     @RequestMapping("/helloexce3")
 7     public ModelAndView HelloExce3() throws MyArrayOutofBoundsException{
 8         if(true){
 9         throw new MyArrayOutofBoundsException();
10         }
11         ModelAndView mav=new ModelAndView();
12         mav.addObject("helloexce","hello exception");
13         mav.setViewName("success");
14         return mav;
15     }

3. Run the test

As follows: Message is the custom information

 

4. Note: @ExceptionHandler and @ResponseStatus two methods to handle exceptions, may not exist.

About anomaly analysis classified as follows: when used, may be more research.

Remark

 Whether a lion or a gazelle, to be run; whether rich or poor, must struggle.

Guess you like

Origin www.cnblogs.com/hsiang/p/11605221.html