|
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. There are two ways to handle the exceptions in the JSP world. First- is the conventional try/catch/finally block in the scriptlet. In this way you have to handle the flow of the program.
The following example will demonstrate the conventional way of error handling.
Example:-
<%try{ String str="11a"; int i = Integer.parseInt(str); }catch(Exception e){ out.println("Error Occured"); }%>
Output:-
The above example will print the "Error occurred" as output. The page is throwing a NumberFormatException while converting string to integer. As a result, the control is transferred to the catch block of the program.
|