Java Reference
In-Depth Information
The following ThrowDemo program is similar to the HandleOrDeclare pro-
gram, except that method1() catches the IOException, does something with it,
and then uses the throw keyword to throw the exception after catching it using
the following statement:
throw e;
Even though method1() catches the IOException, it also throws it; so,
method1() must declare the IOException in order to adhere to the Handle or
Declare Rule.
import java.io.*;
public class ThrowDemo
{
public static void main(String [] args)
{
System.out.println(“Inside main”);
if(args.length == 0)
{
throw new ArrayIndexOutOfBoundsException(5);
}
try
{
method1(args[0]);
}catch(IOException e)
{
System.out.println(“Sorry, but an exception occurred.”);
return;
}
System.out.println(“End of main”);
}
public static void method1(String fileName) throws IOException
{
System.out.println(“Inside method1”);
try
{
method2(fileName);
}catch(IOException e)
{
System.out.println(“Something went wrong!”);
e.printStackTrace();
throw e;
}
System.out.println(“Leaving method1”);
}
public static void method2(String fileName) throws IOException
{
Search WWH ::




Custom Search