आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
JSP - Exception Handling

जब JSP के rules या syntax के खिलाफ अगर program को लिखा जाता है तब program पर exception आने की संभावना होती है |

अगर पूरे JSP Page में कहा पर भी exception हो तो इसकी वजह से बिना exception वाला code भी execute नहीं हो पाता है |

For Example
<%
out.print("Hello World");
out.print(4/0);
%>

ऊपर दिए गए example पर पहला statement तो ठीक है लेकिन दूसरे statement में कुछ exception होने की वजह से पहला statement भी execute नहीं हो पाता है | यह example को try-Catch block पर पढेंगे |


Differrence between Exception and Error

Exception : Exceptions को runtime पर handle किया जा सकता है | exception object को runtime पर throw किया जाता है | अगर user द्वारा कुछ गलत information को provide किया जाता है या network या database से connect नहीं होता है तो exception occur होता है | लेकिन इन exceptions को handle किया जा सकता है |

Error : Errors को handle नहीं किया जा सकता है | Errors बहुत ही critical होता है | Program अगर out of memory जाता है | अगर कहा पर syntax error होता है या system में या web server पर कुछ खराबी होती है तो Error आ जाता है | इन Errors को ठीक करना काफी कठिन काम होता है |


Types of Exceptions in JSP

JSP Page को दो प्रकार से Handle किया जा सकता है |

  1. Page-Level Exception Handling
  2. Try-Catch Block Exception Handling

1. Page-Level Exception Handling in JSP

Page-Level Exception में एक single error page को create किया जाता है | अगर किसी JSP page में कुछ exception होता है तब बिना url change हुए create किये गए errorpage को execute किया जाता है |


Page-Level Exception Example in JSP

index.html
<form action="print.jsp">
Enter Name : <input type="text" name="myname"><br />
Enter Age : <input type="text" name="myage"><br />
<input type="submit" value="Submit">
print.jsp

सबसे पहले अगर Page-Level पर exception को handle करना हो तो handle किये जानेवाले code में page directive का errorPage ये attribute के साथ value के रूप में exception handle करनेवाला code दिया जाता है |

<%@ page errorPage="exception.jsp" %> 
<% 
out.print(request.getParameter("myname"));
out.print(Integer.parseInt(request.getParameter("myage")));
%>
exception.jsp

जब exception handle करनेवाला code लिखा जाता है तब उस code में page directive का isErrorPage ये attribute और उसकी value को true देना जरुरी होता है |

<%@ page isErrorPage="true" %>
<% out.print("Exception : " + exception); %>  


2. Try-Catch Block Exception Handling in JSP

Try-Catch Block में single page पर exception को handle किया जाता है |

Syntax :
try{
	try_block_code;
}catch{
	exception_handling_code;
}

Example :

Example पर पहले statement में Hello World को print किया गया है | इस statement पर कोई exception occur होनी की संभावना नहीं है | लेकिन जिस code पर exception occur होने की संभावना होती है तो उस code को try block के अन्दर दिया जाता है और उसे catch block पर handle किया जाता है |

<%
out.println("Hello World");

try{
out.println(4/0);
}
catch (Exception e){
	out.println("Exception : " + e);
}
%>
Output:
Hello World 
Exception : java.lang.ArithmeticException: / by zero

Try-Catch-finally Block Exception Handling in JSP

finally block पर दिया हुआ code exception occur हो या ना हो ये हमेशा execute होता है |

Syntax :
try{
	try_block_code;
}catch{
	exception_handling_code;
}finally{
	always_executed_code;
}

Example :
<%
out.println("Hello World");

try{
out.println(4/0);
}
catch (Exception e){
	out.println("Exception : " + e);
}finally{
	out.println("Always executed.")
}
%>
Output:
Hello World 
Exception : java.lang.ArithmeticException: / by zero
Always executed.