Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Use multiple catch statement in single jsp 
 

In java a single try can have multiple catch statements. The code bound by the try block need not always throw a single exception so we can use multiple catches or Exception class (base class of all exceptions).

 

Use multiple catch statement in single jsp

                         

In java a single try can have multiple catch statements. The code bound by the try block need not always throw a single exception so we can use multiple catches or Exception class (base class of all exceptions). When exception occurs while executing any statement within try block, control enters into the catch blocks until it finds its matching block.

If catch block containing the Exception class object then the subsequent catch blocks will not be executed. If the appropriate exception object is not found and Exception class is also not used in program, the java complier gives an error stating that the subsequent catch blocks have not been reached. This is known as Unreachable code problem. To resolve this, the last catch block in multiple catch blocks must contain the Exception class object.

The example below 'multi_catches_jsp.jsp' will show you how to use multiple catch blocks to handle multiple exceptions. This program encounters problem with arithmetic calculation , so throwing Arithmetic Exception. To handle any other type of exception which has not been handled, we have used catch block with Exception object.

multi_catches_jsp.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<HTML>
    <HEAD>
        <TITLE>multiple catch in single jsp program</TITLE>
    </HEAD>
    <BODY>
	<% try {
		int arithmeticExp = 10/0;
       }
           // Multiple catch statement
	   catch(ArithmeticException aEx){
		   out.println("Arithmetic Exception : "+aEx);
	   }
	   catch(Exception ex){
		  out.println("Exception : "+ex);
	   }
	%>
    </body> 
</html>

Save this code as a .jsp file named "multi_catches_jsp.jsp" in your application directory ('user' in our example) in Tomcat server and run this jsp page with url http://localhost:8080/user/multi_catches_jsp.jsp in address bar of the browser.

Download Source Code

                         

» View all related tutorials
Related Tags: c jsp ide variables script io methods scope count method variable ip page vi number lock ole int ria this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.