Example: This program also handles the Checked Exceptions. The following program takes names of two files as input and copies the file specified by the first argument to the file specified by the second argument. The number of bytes copied are displayed in the finally block. So number of bytes copied will be displayed even if some abnormal condition terminates the program. The files are also closed in the finally block so that they will always be closed.
It has three handlers to handle the abnormal conditions:
class MyCopy
{ public static void main(String args[])
{ int byte_count = 0;
byte buffer[] = new byte[512];
String input_file = null;
String output_file = null;
FileInputStream fin = null;
FileOutputStream fout = null;
try
{ input_file = args[0];
output_file = args[1];
fin = new FileInputStream(input_file);
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
}
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println("Use:java MyCopy source target");
}
catch(FileNotFoundException e)//Checked Exception
{ System.out.println("Can't open input file:"+input_file);
}
byte_count = byte_count + bytes_in_one_read;
catch(IOException e) //Checked Exception
{ System.out.println("I/O Exception occurs");
}
finally
{ if(byte_count > 0)
if(fin != null)
if(fout != null)
}
System.out.println(byte_count + " bytes written");
fin.close();
fout.close();
} // end main
} // end class
Note:
1. FileNotFoundException and IOException are checked exceptions and must be handled.
2. It is possible that we handle only IOException as this will also indirectly handle the FileNotFoundException, which is its sub-class.
3. If both IOException and FileNotFoundException are handled then FileNotFoundException must be caught before IOException as it is sub-class of IOException.
Example: The previous example is rewritten to use method:
int copyFile(String input-file, String output-file)
We want that caller of this method should handle the abnormal conditions by itself. Method will not have any error handler for I/O Exception. A throws clause is added to the method declaration to indicate that method can throw exception and caller has to handle this exception. The caller can also throw the exception if it does not want to handle itself.
import java.io.*;
class MyCopy1
{ static FileInputStream fin = null;
static FileOutputStream fout = null;
public static int CopyFile(String input_file, String output_file)
{ int byte_count = 0;
throws IOException, FileNotFoundException
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
byte buffer[] = new byte[512];
fin = new FileInputStream(input_file);
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
byte_count = byte_count + bytes_in_one_read;
import java.io.*;
class MyCopy1
{ static FileInputStream fin = null;
static FileOutputStream fout = null;
public static int CopyFile(String input_file, String output_file)
{ int byte_count = 0;
throws IOException, FileNotFoundException
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
byte buffer[] = new byte[512];
fin = new FileInputStream(input_file);
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
byte_count = byte_count + bytes_in_one_read;
{ int a = args.length;
int b = 42/a;
System.out.println("a = " + a);
try
{ if(a == 1)
a = a/(a-a);
if(a == 2)
{
int c[] = {1};
c[42] = 99;
}
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println("Array index out-of-bounds:"+e);
}
System.out.println("Outer Try");
}
catch(ArithmeticException e)
{ System.out.println(e);
}
System.out.println("After Outer Try");
}
}
Note: Nesting of try statement can occur in less obvious ways when method calls are involved.
For example, you can enclose a call to a method within a try block. Inside that method is another try statement. In this case, the try within the method is still nested inside the outer try block, which calls the method.
Example: Previous program is re-written so that the nested try block is moved inside the method nesttry().
class NestTry1
{ static void nesttry(int a)
{ try
{ if(a == 1)
if(a == 2)
{ int c[] = {1};
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out-of-bounds:" + e);
}
a = a/(a-a);
c[42] = 99;
}
public static void main(String args[])
{ try
{ int a = args.length;
int b = 42/a;
System.out.println("a = " + a);
nesttry(a);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}
It has three handlers to handle the abnormal conditions:
- One handler will print the usage of the program when the user does not provide both input and output file names.
- The next handler will notify the user when the input file does not exist.
- Another handler will print an error message when other I/O exceptions occur.
class MyCopy
{ public static void main(String args[])
{ int byte_count = 0;
byte buffer[] = new byte[512];
String input_file = null;
String output_file = null;
FileInputStream fin = null;
FileOutputStream fout = null;
try
{ input_file = args[0];
output_file = args[1];
fin = new FileInputStream(input_file);
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
}
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println("Use:java MyCopy source target");
}
catch(FileNotFoundException e)//Checked Exception
{ System.out.println("Can't open input file:"+input_file);
}
byte_count = byte_count + bytes_in_one_read;
catch(IOException e) //Checked Exception
{ System.out.println("I/O Exception occurs");
}
finally
{ if(byte_count > 0)
if(fin != null)
if(fout != null)
}
System.out.println(byte_count + " bytes written");
fin.close();
fout.close();
} // end main
} // end class
Note:
1. FileNotFoundException and IOException are checked exceptions and must be handled.
2. It is possible that we handle only IOException as this will also indirectly handle the FileNotFoundException, which is its sub-class.
3. If both IOException and FileNotFoundException are handled then FileNotFoundException must be caught before IOException as it is sub-class of IOException.
Example: The previous example is rewritten to use method:
int copyFile(String input-file, String output-file)
We want that caller of this method should handle the abnormal conditions by itself. Method will not have any error handler for I/O Exception. A throws clause is added to the method declaration to indicate that method can throw exception and caller has to handle this exception. The caller can also throw the exception if it does not want to handle itself.
import java.io.*;
class MyCopy1
{ static FileInputStream fin = null;
static FileOutputStream fout = null;
public static int CopyFile(String input_file, String output_file)
{ int byte_count = 0;
throws IOException, FileNotFoundException
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
byte buffer[] = new byte[512];
fin = new FileInputStream(input_file);
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
byte_count = byte_count + bytes_in_one_read;
import java.io.*;
class MyCopy1
{ static FileInputStream fin = null;
static FileOutputStream fout = null;
public static int CopyFile(String input_file, String output_file)
{ int byte_count = 0;
throws IOException, FileNotFoundException
fout = new FileOutputStream(output_file);
int bytes_in_one_read;
byte buffer[] = new byte[512];
fin = new FileInputStream(input_file);
while((bytes_in_one_read = fin.read(buffer)) != -1)
{ fout.write(buffer,0,bytes_in_one_read);
byte_count = byte_count + bytes_in_one_read;
{ int a = args.length;
int b = 42/a;
System.out.println("a = " + a);
try
{ if(a == 1)
a = a/(a-a);
if(a == 2)
{
int c[] = {1};
c[42] = 99;
}
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println("Array index out-of-bounds:"+e);
}
System.out.println("Outer Try");
}
catch(ArithmeticException e)
{ System.out.println(e);
}
System.out.println("After Outer Try");
}
}
Note: Nesting of try statement can occur in less obvious ways when method calls are involved.
For example, you can enclose a call to a method within a try block. Inside that method is another try statement. In this case, the try within the method is still nested inside the outer try block, which calls the method.
Example: Previous program is re-written so that the nested try block is moved inside the method nesttry().
class NestTry1
{ static void nesttry(int a)
{ try
{ if(a == 1)
if(a == 2)
{ int c[] = {1};
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out-of-bounds:" + e);
}
a = a/(a-a);
c[42] = 99;
}
public static void main(String args[])
{ try
{ int a = args.length;
int b = 42/a;
System.out.println("a = " + a);
nesttry(a);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}