Related Tutorials


 
 

Share on Google+Share on Google+

JSTL

Advertisement
If you have written some code which you think that it can invoke a error, and still you are confident that you can recover from that solution by yourself. Then then is one tag provided to you that is which is one of the tag of core action librar

JSTL <c: catch>

        

If you have written some code which you think that it can invoke a error, and still you are confident that you can recover from that solution by yourself. Then then is one tag provided to you that is <c:catch> which is one of the tag of core action library. It works like a try/catch block in java. 

If we have any doubt that the particular code can invoke a error then we should write those codes inside the <c:catch> core action tag. The beauty of this tag is that it works both like a try and catch. There is no such thing like try tag. This tag can handle both the try and catch situation. 

In this example we are going to use the var attribute of the <c:catch> core action. This attribute is optional that means it is not mandatory. This attribute is used when we want to access the exception after the end of the <c:catch> tag.  In this example we have also used one more tag that is <c:if>, which we are using to check the condition. 

The code of the program is given below:

 

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>
<title>Catching the Exception</title>
</head>
<body>
<strong>I can catch the exception:</strong><br>
<c:catch var ="catchException">
The exception will be thrown inside the catch:<br>
<% int x = 5/0;%>
</c:catch>

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

</body>
</html>

The output of the program is given below:

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Related Tutorials

Discuss: JSTL   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com