onaled777
asked on
Exception Handling in Java at the controller level
I have some NumberFormatExceptions being thrown at the Controller level during some New Relic testing and I am trying to eliminate them. My UI calls my controller methods directly
I have a doing some research on this and concluded that the best way to handle these exception is by including a try/catch in the controller method for the NumberFormatException and then throwing it back as a Runtime Exception.
In addition I will include checks at the UI and the controller level to ensure that values are truly numeric when they are expected to be.
My question is, is this inline with best practices for Exception Handling? Are there any changes and/or improvements that you would suggest?
I have a doing some research on this and concluded that the best way to handle these exception is by including a try/catch in the controller method for the NumberFormatException and then throwing it back as a Runtime Exception.
//I would include this around the body of the code in the controller.
try{
}catch(NumberFormatExcepti<wbr ></wbr>on ne){
throw new RuntimeException(ne);
}
In addition I will include checks at the UI and the controller level to ensure that values are truly numeric when they are expected to be.
//In jquery I would use the isNumeric validation for each variable that will be a parameter passed to the controller.
if (!$('#someObjID').isNumeric()){
$("#searchresults").html("<BR>someObjID needs to be numeric");
$("#searchresults").addClass ("validation_color");
return false;
}
//at the controller level I would also have similar numeric value checks
if (StringUtil.isNumeric(request.getParameter("someObjID"))){
model.put("errorMessage", "someObjID needs to be numeric.");
return new ModelAndView ("searchresults",model);
}
My question is, is this inline with best practices for Exception Handling? Are there any changes and/or improvements that you would suggest?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER