SPRINGMVC Unified Exception Handling

Source: Internet
Author: User

 Public classCustomexceptionresolverImplementsHandlerexceptionresolver {//The front-end controller Dispatcherservlet executes this method during handlermapping, calling Handleradapter execution handler, if an exception is encountered//handler final to execute the handler, its true identity is Handlermethod//Exception Ex is receiving abnormal information .@Override PublicModelandview resolveexception (httpservletrequest request, httpservletresponse response, Object handler, Ex Ception ex) {//Output ExceptionEx.printstacktrace (); //Uniform exception Handling code//for System-Customized customexception exceptions, you can get exception information directly from the exception class and treat the exception in the error page//Exception InformationString message =NULL; Customexception customexception=NULL; //If ex is a system-defined exception, remove the exception information directly        if(exinstanceofcustomexception) {customexception=(Customexception) ex; }Else{            //For non-customexception exceptions, this class is reconstructed into a customexception with the exception message "Unknown error"Customexception =NewCustomexception ("Unknown error"); }                //error MessageMessage =Customexception.getmessage (); Request.setattribute ("Message", message); Try {            //turn to error pageRequest.getrequestdispatcher ("/web-inf/jsp/error.jsp"). Forward (request, response); } Catch(servletexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                return NewModelandview (); }}

SPRINGMVC Unified Exception Handling

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.