Java Tomcat Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP  Java Tomcat Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Tomcat Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Tomcat Application Hosting Web Hosting website hosting, web site hosting, web page hosting Apache, PHP, MySQL, PERL, servlets Java, JSP,Python Java Tomcat Application Hosting Web Hosting website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP,Python
Java Tomcat Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, JSP, Python Java Tomcat Application Hosting Web Hosting, website hosting, web site hosting, web page hosting, Apache, PHP, MySQL, PERL, servlets Java, Python,JSP
Java Tomcat Application Hosting Web Hosting Sign-Up Java Tomcat Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting  Java Tomcat Application Hosting Resellers web hosting, website hosting, web site hosting Java Tomcat Application Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Java Tomcat Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python,JSP,  Fundraising
Java Tomcat Application Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting
WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Java Tomcat Application Hosting Welcome Java Tomcat Application Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Java Tomcat Application Hosting Fund Raising, Fundraising, web hosting Java Tomcat Application Hosting Resellers, web Hosting Java Tomcat Application Hosting Web Design, web Hosting Java Tomcat Application Hosting Extra Services,  web Hosting Java Tomcat Application Hosting Traffic Booster, web hosting Java Tomcat Application Hosting Traffic Booster, web hosting Java Tomcat Application Hosting Technical Support,  web Hosting Java Tomcat Application Hosting webmaster tips,  web Hosting Java Tomcat Application Hosting 30 Day Money Back, web hosting Java Tomcat Application Hosting Legal Notices for Web Hosting Java Tomcat Application Hosting Glossary Computer Terms for web Hosting Java Tomcat Application Hosting Contact Information - web hosting

Site Map
Java Tomcat Application Hosting Web Hosting, website hosting, web site hosting , web page hosting Apache, PHP, MySQL, PERL, servlets Java, Python, JSP Java Tomcat Application Hosting Java Tomcat Application Hosting Java Tomcat Application Hosting Java Tomcat Application Hosting Java Tomcat Application Hosting Specifying the Exceptions Thrown by a Method (The Java™ Tutorials > Essential Classes > Exceptions)
Trail: Essential Classes
Lesson: Exceptions
Home Page > Essential Classes > Exceptions
Specifying the Exceptions Thrown by a Method
The previous section showed how to write an exception handler for the writeList method in the ListOfNumbers class. Sometimes, it's appropriate for code to catch exceptions that can occur within it. In other cases, however, it's better to let a method further up the call stack handle the exception. For example, if you were providing the ListOfNumbers class as part of a package of classes, you probably couldn't anticipate the needs of all the users of your package. In this case, it's better to not catch the exception and to allow a method further up the call stack to handle it.

If the writeList method doesn't catch the checked exceptions that can occur within it, the writeList method must specify that it can throw these exceptions. Let's modify the original writeList method to specify the exceptions it can throw instead of catching them. To remind you, here's the original version of the writeList method that won't compile.

// Note: This method won't compile by design!
public void writeList() {
    PrintWriter out = 
               new PrintWriter(new FileWriter("OutFile.txt"));
    for (int i = 0; i < SIZE; i++) {
        out.println("Value at: " + i + " = " 
                     + vector.elementAt(i));
    }
    out.close();
}
To specify that writeList can throw two exceptions, add a throws clause to the method declaration for the writeList method. The throws clause comprises the throws keyword followed by a comma-separated list of all the exceptions thrown by that method. The clause goes after the method name and argument list and before the brace that defines the scope of the method; here's an example.
public void writeList() throws IOException,
                               ArrayIndexOutOfBoundsException {
Remember that ArrayIndexOutOfBoundsException is an unchecked exception; including it in the throws clause is not mandatory. You could just write the following.
public void writeList() throws IOException {
Previous page: Putting It All Together
Next page: How to Throw Exceptions
 
 
 

Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . . .
.
.
. .
.
. .
. . . . . . . . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting