The code which can throw exception should be written in the try block. If the exceptions occurs at that particular block then it will be catch by the catch block. We can have more than one try/catch block. We can declare multiple try blocks inside the try block. The most specific exception which can be thrown is written on the top in the catch block following by the less specific least.
The code of the program is given program:
<HTML>
<HEAD>
<TITLE>Nesting try/catch Statements in jsp</TITLE>
</HEAD>
<BODY>
<FONT SIZE="5" COLOR="#666600">Nesting try/catch Statements in jsp</FONT>
<%
try {
int c[] = {0, 1, 2, 3};
try {
c[5] = 5;
} catch(ArrayIndexOutOfBoundsException e) {
out.println("<br>Array index out of bounds: " + e);
}
int a=c[3]/c[0];
} catch(ArithmeticException e) {
out.println("<br>Divide by zero: " + e);
}
%>
</BODY>
</HTML>
Output of the Program:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Nested try catch
Post your Comment