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 which is one of the tag of core action librar

Catching Exception usin c: catch

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.