| Exception Handling in JSP Pages |
| |
| Improved FormHandler.jsp page |
| |
| Remove the FormHandler code in FormHandler.jsp page and add the following one : |
| |
<%
int age;
try {
age = Integer.parseInt(request.getParameter("age"));
} catch (NumberFormatException e) {
throw new JspException("Please enter a valid integer value!");
}
%> |
| |
| Explanation |
| This time we catch the NumberFormatException locally and throw a new JspException with our own exception message. JspException is a JSP special exception class which extends java.lang.Exception. So all we had to do is to give our own message to the JspException class. Now lets see what will happen this time. |
| |
| We will just have to edit one single line in ExceptionHandler.jsp for it to work. |
| |
| Improved ExceptionHandler.jsp |
| Simply change the exception handler code with this one : |
<font color="red">
<%= exception.getMessage() %><br>
</font>
|
| That's it. Now view the Form.php page again and don't enter any value and press the submit button. |
| |
| This time we will not see "java.lang.NumberFormatException", rather the message will be : |
| Please enter a valid integer value! |
| |
| And like before we can see the complete stack trace by viewing the source for the FormHandler.jsp page in your browser. |
| |
| |
|
| |
| |