title: Catch parameter parsing exception in Spring 3.0 WebMVC
body: <p>I use Spring WebMVC to provide a REST API. I use methods like</p>
  
  <p><code>@RequestMapping("/path({id}") void getById(@PathVariable("id") int id) {}</code> methods.</p>
  
  <p>When the client incorrectly put a string instead of an integer id into the query, I get a NumberFormatException like:</p>
  
  <pre><code>java.lang.NumberFormatException: For input string: "dojo"
      at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
      at java.lang.Long.parseLong(Long.java:410)
      at java.lang.Long.valueOf(Long.java:525)
      at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:158)
      at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:59)
      at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:1)
      at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:420)
      at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:135)
      at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
      at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:104)
      at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
      at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:526)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:602)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:289)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
  </code></pre>
  
  <p>My Question is now, how can I elegantly catch it? I know that Spring provides @ExeptionHandler annotations but I don't want to catch the NFE in general. I want to be able to catch all parsing exception in order to present a nice error message to the client.</p>
  
  <p>Any ideas?</p>
  
  <p>Cheers,</p>
  
  <p>Jan</p>