Exception Handaling In Spring
--------------------------------------------------------------------------------------------------------------------------
- When The User Request for the web page then user get webpage as a Response.
- Example
@Controller
public class CommonController {
@RequestMapping("/")
public String homePage() {
return "home";
}
-------------------------------------------------------------------------------------------------------------------------------
- Due to some developer mistake or some other reason if while processing the request if the method throws some exception say Null Pointer , Resourcefulness or Any Other what user get in that case is ".
@Controller
public class CommonController {
@RequestMapping("/")
public String homePage() {
String exception_occured="NullPointerExceptoin";
if(exception_occured.equalsIgnoreCase("NullPointerExceptoin")){
throw new NullPointerException("Exceptoin NUll");
}
return "home";
}
@ExceptionHandler(value = NullPointerException.class)
public String handleNullPointerException(){
return "NullPointerException";
// error jsp page name(NullPointerExceptoin.jasp)
}
@ExceptionHandler(value = IOException.class)
public String handleIOException(){
return "IoExcetion";
}
}
-------------------------------------------------------------------------------------------------------------------------------
Generic Exception :
Whatever Exceptions you thing the controller class can throw at run time while processing the users request u simply need to add exceptoin handler method for all those exception
The important point the note here is that ,
your controller class might throw a lot of exception at run time and it may not be so convenient for u to write exceptoin handler method for each exception.
So handle such a problem we handle Generic Exception
@ExceptionHandler(value = Exception.class)
public String handleIOException(){
return "Excetion";
}
Create Exception.jsp page in application.
-------------------------------------------------------------------------------------------------------------------------------
Now We learn how to handle Exception globally at one place for all controller classes without writing exceptoin handler method individually for each controller .
Spring mvc says hey developers , you include the class in your application with @ControllerAdvice annotation on its top then whatever exceptoin handler methods you put into such a class all those would apply to all Controller classes which stays on the application
@ControllerAdvice
public class GlobalExceptoinHandler {
@ExceptionHandler(value = NullPointerException.class)
public String handleIOException(){
return "NullPointerException";
}
@ExceptionHandler(value = IOException.class)
public String handleNullPointerException(){
return "IoExcetion";
}
}
-----------------------------------------------------------------------------------------------------------------------------
If we observe each method here in this class what we are doing first we locking information of thrown exception and then returning view name and this is what we doing in exceptoin handle method
Spring mvc says hey developer if u want to perform just thease two task then no need to write such a big java class in your applicatoin
u can perform all such a handling task by writing
- When The User Request for the web page then user get webpage as a Response.
- Example
@Controller
public class CommonController {
@RequestMapping("/")
public String homePage() {
return "home";
}
-------------------------------------------------------------------------------------------------------------------------------
- Due to some developer mistake or some other reason if while processing the request if the method throws some exception say Null Pointer , Resourcefulness or Any Other what user get in that case is ".
HTTP Status 500 -
Brief description of the error which occurred on server.
and related stack trace information.
- Example
@Controller
public class CommonController {
@RequestMapping("/")
public String homePage() throws IOException{
String exception_occurred="NullPointerException";
if(exception_occurred.equalsIgnoreCase("NullPointerException")){
throw new NullPointerException("Page not Fouund");
}
return "home";
}
public class CommonController {
@RequestMapping("/")
public String homePage() throws IOException{
String exception_occurred="NullPointerException";
if(exception_occurred.equalsIgnoreCase("NullPointerException")){
throw new NullPointerException("Page not Fouund");
}
return "home";
}
------------------------------------------------------------------------------------------------------------------------------------
Normally when exception occurs for which there is no exception handling code is written in controller class the application by default sends error page like above.
Now many times developer would like to send customize error web page instead of this default error web page which doesn't seems so user friendly.
So question is what task i will need perform in applications so that i would able to send a customize Error web page when this method throw Null Pointer exception while performing request.
------------------------------------------------------------------------------------------------------------------------------
So to tackle this requirement we just need to perform two simple steps
First i will need to include an Error web page NullPointerExceptoin.jsp page in application which i would like to show to the user.
I would need to include a method in a controller nullPointerExceptoinHandaler() method which has @ExceptoinHandler annotation on top of the method.
Example :
public class CommonController {
@RequestMapping("/")
public String homePage() {
String exception_occured="NullPointerExceptoin";
if(exception_occured.equalsIgnoreCase("NullPointerExceptoin")){
throw new NullPointerException("Exceptoin NUll");
}
return "home";
}
@ExceptionHandler(value = NullPointerException.class)
public String handleNullPointerException(){
return "NullPointerException";
// error jsp page name(NullPointerExceptoin.jasp)
}
@ExceptionHandler(value = IOException.class)
public String handleIOException(){
return "IoExcetion";
}
}
-------------------------------------------------------------------------------------------------------------------------------
Generic Exception :
Whatever Exceptions you thing the controller class can throw at run time while processing the users request u simply need to add exceptoin handler method for all those exception
The important point the note here is that ,
your controller class might throw a lot of exception at run time and it may not be so convenient for u to write exceptoin handler method for each exception.
So handle such a problem we handle Generic Exception
@ExceptionHandler(value = Exception.class)
public String handleIOException(){
return "Excetion";
}
Create Exception.jsp page in application.
-------------------------------------------------------------------------------------------------------------------------------
Now We learn how to handle Exception globally at one place for all controller classes without writing exceptoin handler method individually for each controller .
Spring mvc says hey developers , you include the class in your application with @ControllerAdvice annotation on its top then whatever exceptoin handler methods you put into such a class all those would apply to all Controller classes which stays on the application
@ControllerAdvice
public class GlobalExceptoinHandler {
@ExceptionHandler(value = NullPointerException.class)
public String handleIOException(){
return "NullPointerException";
}
@ExceptionHandler(value = IOException.class)
public String handleNullPointerException(){
return "IoExcetion";
}
}
-----------------------------------------------------------------------------------------------------------------------------
If we observe each method here in this class what we are doing first we locking information of thrown exception and then returning view name and this is what we doing in exceptoin handle method
Spring mvc says hey developer if u want to perform just thease two task then no need to write such a big java class in your applicatoin
u can perform all such a handling task by writing
Comments
Post a Comment