Link to home
Start Free TrialLog in
Avatar of onaled777
onaled777Flag for United States of America

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.

//I would include this around the body of the code in the controller.
try{

}catch(NumberFormatExcepti<wbr ></wbr>on ne){
      throw new RuntimeException(ne);
}

Open in new window


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;
	}

Open in new window


//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); 
}

Open in new window


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
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of onaled777

ASKER

Just grouping I believe.   Thanks for your time.