如何捕捉java.lang.NumberFormatException.forInputString

2019-07-18 06:06发布

我在我的JSP页面复选框接受整数值:

<form:checkbox path="somePath" value="2" /> Dangerous Checkbox <br />

如果用户改变输入到的值String值,例如:

<form:checkbox path="somePath" value="blah" /> Dangerous Checkbox <br />

该页面将抛出一个NumberFormatException 。 我如何能赶上这在我的控制和显示有意义的信息?

Answer 1:

您可以使用JSTL的C:抓标签:

<c:catch var ="numberFormatException">
<form:checkbox path="somePath" value="blah" /> Dangerous Checkbox <br />
</c:catch>

<c:if test = "${numberFormatException!= null}">
   <p>The exception is : ${numberFormatException} <br />
   There is an exception: ${numberFormatException.message}</p>
</c:if>


文章来源: How to catch java.lang.NumberFormatException.forInputString Exception in Controller?