Nested try catch

Ads
 

Nested try catch

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.

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.

Nested try catch

        

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:

Download this example

Ads

Ads