In the process of system development, exception handling is inevitable. If the exception handling is not good, it will cause users a bad experience. This paper mainly explains the relevant knowledge points of exception handling in Spring MVC development process. It is only for learning and sharing. If there are shortcomings, please correct them.
Summary
In Spring Mvc, there are several ways to handle exceptions. This paper mainly describes two schemes:
- Exception Handler is used to capture, receive and process exceptions.
- The @ResponseStatus annotation is used to customize the content of the return status code.
Through the @ExceptionHandler operation step
1. Customize an exception class that inherits from Exception class
As follows: @ExceptionHandler annotates the method, indicating that this method can be used to handle exceptions, separated by commas if multiple exceptions need to be captured.
If you need to catch exceptions from other classes, you need to add the @Controller Advice annotation to 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 * Custom exception 9 * @author Administrator 10 * 11 */ 12 13 @ControllerAdvice 14 public class MyException extends Exception { 15 16 /** 17 * This method can capture Arithmetic Exception exceptions thrown in this class and support multiple exceptions. 18 * @param ex To capture the same, this method must have only one parameter, and if there are other types of parameters, errors will be reported. 19 * @return 20 */ 21 @ExceptionHandler({ArithmeticException.class,ArrayIndexOutOfBoundsException.class,MyArrayOutofBoundsException.class}) 22 public ModelAndView handlerException(Exception ex){ 23 //take ex Information output in the background 24 System.out.println(ex.getMessage()); 25 //Display error messages in the front desk 26 ModelAndView mav=new ModelAndView(); 27 mav.setViewName("error"); 28 mav.addObject("exce", ex); 29 return mav; 30 } 31 }
2. Set a method and throw a mathematical exception, then you can catch it and display it on the 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 * Throw an exception 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. Running test
Output error page, as follows:
Through @ResponseStatus, return error information, operation steps
1. Customize an exception class and add the @ResponseStatus annotation
As follows: value represents the status code, is an enumeration type, and reason displays the 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 It can be expressed in front of a class or in front of a method. 10 * 11 */ 12 @ResponseStatus(value=HttpStatus.FORBIDDEN,reason="Page No Access 22222") 13 public class MyArrayOutofBoundsException extends Exception { 14 15 }
2. Define a method to throw the exception
As follows:
1 /** 2 * Test the third exception 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. Running test
As follows: Message is custom information
4. Note that @ExceptionHandler and @ResponseStatus are two ways of handling exceptions, and they cannot exist at the same time.
The classification of exception resolution is as follows: when you use it, you can study it more.
Remarks
Whether it's a lion or an antelope, run; whether it's poor or rich, struggle.