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?
onaled777Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
I'm a bit confused about what you're attempting to do to solve this.

A NumberFormatException is already a RuntimeException
(http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html)

so catching it and rethrowing it wrapped into a new RuntimeException doesn't seem to be doing much?

If you don't want the exception to be coming from the controller - it seems like you'd want to move the validation (and the triggering of the exception) to a validation step that comes when the input is received from the UI and before it's passed to the controller?  It really depends on where you want to do your validation - as part of your UI code or within the controller after the UI hands it off.  You'd still be triggering an exception, but it wouldn't be inside the controller if you check for it sooner.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
onaled777Author Commented:
Just grouping I believe.   Thanks for your time.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.