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