SPRINGMVC Correlation-Exception handling

Source: Internet
Author: User

One, based on the @exceptionhandler annotation exception handling method:

1. Add <mvc:annotation-driven> Tag: (the label is standard, usually carried during development)
2. Define the method that is decorated by @exceptionhandler annotations in the current handler for handling exception information!

@ExceptionHandler (value={exception type. class})

@ExceptionHandler (value={exception type. class, exception type. Class,........})
Attention:
[Email protected] method decorated in the parameter can be added to the exception type of parameters, which corresponds to the occurrence of the exception information

1         @ExceptionHandler (value={arithmeticexception.  Class})2public       String Excptionhandler (Exception ex) {3         System.out.println (ex); 4         return "Error"; 5     }6//Exception ex As the entry parameter, you can display the exception object information to the console
Example

The [email protected] method cannot be passed into the map. If you want to upload the exception information to the page, you need to use Modelandview as the return value of the method. (also not available in model)

1 in the class:2@ExceptionHandler (value={arithmeticexception.class})3      PublicModelandview Excptionhandler (Exception ex) {4Modelandview mv=NewModelandview ();5Mv.setviewname ("Error");6Mv.addobject ("Exception", ex);7         returnMV;8     }9 error page:Ten One${exception}
Example

[Email protected] annotation defines the method priority issue:
For example, nullpointerexception occurs, but the declared exception has runtimeexception and Exception, which finds the least inherited depth @ExceptionHandler annotation method based on the most recent inheritance of the exception. The method that marks the RuntimeException

1@ExceptionHandler (value={exception.class})2        Public voidExcptionHandler1 () {3SYSTEM.OUT.PRINTLN ("Exception occurred"); 4           5       }6       7@ExceptionHandler (value={arithmeticexception.class})8        PublicString Excptionhandler () {9SYSTEM.OUT.PRINTLN ("Exception occurred");Ten          return"Error"; One      } A       -@RequestMapping (value= "/testexception") -       PublicString testexception (@RequestParam (value= "I") (Integer i) { theSYSTEM.OUT.PRINTLN (10/i); -          return"Success"; -      }   -  //the type of run exception on the annotation compares to the actual exception type, which is the most accurate (small in scope, closest to) performing exception handling
@ExceptionHandler Annotation Multi-exception method processing

4.ExceptionHandlerMethodResolver inside if cannot find @exceptionhandler annotation, will find @controlleradvice in the @exceptionhandler annotation method

Handling of all handler anomalies:

1 @ControllerAdvice2  Public classhandlerexception {3     4@ExceptionHandler (value={arithmeticexception.class})5      PublicString handleexception () {6         7         return"Error";8     }9 }Ten //All handler arithmeticexception anomalies are handled uniformly by the above function.
Global Exception Handling

Attention:

In the program execution processing class has the exception handling method, encounters the exception program to take precedence in this class the exception processing method,

If not, you will find the @exceptionhandler annotation method in the @controlleradvice annotation class.

1. @ControllerAdvice Annotation Class: Handles global exceptions.

2, exception handling: Always take precedence over the methods in this class.

1 in the JSP page:2<a href= "${pagecontext.request.contextpath}/testexception?i=2" > Exception handling </a>3 in the same class:4@ExceptionHandler (value={arithmeticexception.class})5      PublicString Excptionhandler () {6SYSTEM.OUT.PRINTLN ("Exception occurred");7         return"Error";8     }9     Ten@RequestMapping (value= "/testexception") One      PublicString testexception (@RequestParam (value= "I") (Integer i) { ASYSTEM.OUT.PRINTLN (10/i); -         return"Success"; -     }   the //Change the URL i = 2 to i = 0 o'clock exception, the method of annotation to go to the error page! 
Basic Example @ExceptionHandler exception handling

Second, configuration-based exception handling:
If you want to uniformly handle all exceptions, you can use Simplemappingexceptionresolver, which maps the exception class name to the view name, that is, the exception is reported with the corresponding view when an exception occurs

1  <Beanclass= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">2             <!--specifies the key required to obtain the exception information in the request domain: Ex -3             < Propertyname= "Exceptionattribute"value= "Ex"></ Property>4             <!--Specifying Exception Mappings -5             < Propertyname= "Exceptionmappings">6             <!--Property Common terms keys and values are string types -7                 <Props>8                    <!--The key property of the prop tag specifies the full class name of the exception that occurs, specifying the page to which the exception occurs by value!  -9                     <propKey= "Java.lang.ArithmeticException">Error</prop>Ten                 </Props> One             </ Property> A         </Bean>    
Exception handling configuration information

SPRINGMVC Correlation-Exception handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.