Home Jsp Simple-jsp-example Catching Exception usin c: catch
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Catching Exception usin c: catch

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

Catching Exception usin 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 using the scriptlet inside the <c:action>. The code inside the scriptlet will definitely throw an exception. The exception will be caught inside the <c:catch> core action tag instead of sending it to the error page. 

The code of the program is given below:

 

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ page errorPage="JSTLErrorPage.jsp"%>
<html>
<head>
<title>Catching a risky code</title>
</head>
<body>
<c:catch>
Division by 0 will definately cause an exception<br>
<% int x = 5/0;%>
</c:catch>

If we come across this c:action tag that means we have overcome the exception. 
</body>
</html>

The output of the program is given below:

Download this example.

        

Advertisement

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

Ask Questions?    Discuss: Catching Exception usin c: catch  

Post your Comment


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