09. Spring - Exception handling globally.
Exception
handling globally.
//////////////////////////////////
--------- Extra code for custom
exception.----------- //////////////////////////////////
Understand bellow code to get
understand about how java custom exceptions can be handled.
public class Test {
public static void main(String[] args) throws Exception {
try{
myMethod();
}catch(Exception e){
System.out.println(e);
}
}
static void myMethod() throws MyException {
throw new MyException("Custom occured");
}
}
class MyException extends Exception {
MyException(String s) { super(s);}
}
///////////////////////////////// ----------------------------------------------------------------- //////////////////////////////////
@ControllerAdvice – Every controller get advice
from this class.
Create a custom exception
handle class and global exception handler class and jsp page for show
exception.
@Controller
publicclassPageController {
@RequestMapping(value = "/show/{id}/product")
Public ModelAndView showSingleProduct(@PathVariableint id) throws ProductNotFoundException {
return mv;
}
}
import java.io.PrintWriter;
import java.io.StringWriter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handlerNoHandlerFoundException() {
ModelAndView mv = new ModelAndView("error");
mv.addObject("errorTitle", "The page is not constructed!");
mv.addObject("errorDescription", "The page you are looking for is not available now!");
mv.addObject("title", "404 Error Page");
return mv;
}
@ExceptionHandler(ProductNotFoundException.class)
public ModelAndView handlerProductNotFoundException() {
ModelAndView mv = new ModelAndView("error");
mv.addObject("errorTitle", "Product not available!");
mv.addObject("errorDescription", "The product you are looking for is not available right now!");
mv.addObject("title", "Product Unavailable");
return mv;
}
@ExceptionHandler(Exception.class)
public ModelAndView handlerException(Exceptionex) {
ModelAndView mv = new ModelAndView("error");
mv.addObject("errorTitle", "Contact Your Administrator!!");
/* only for debugging your application*/
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
mv.addObject("errorDescription", sw.toString());
mv.addObject("title", "Error");
return mv;
}
}
import java.io.Serializable;
public class ProductNotFoundException extends Exception implements Serializable {
private static final long serialVersionUID = 1L;
private String message;
public ProductNotFoundException() {
this("Product is not available!");
}
Public ProductNotFoundException(String message) {
this.message = System.currentTimeMillis() + ": " + message;
}
Public String getMessage() {
return message;
}
}
Do modification in web.xml like bellow.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- FOr throwing exception -->
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
Comments
Post a Comment