Java Reference
In-Depth Information
exceptions must be handled; otherwise, it would be necessary to append a throws
IOException clause to the main() method header.
Youcanspecifyatrystatementwithonlyafinallyblock.Youwoulddosowhenyou
arenotpreparedtohandleanexceptionintheenclosingmethod(orenclosingtrystate-
ment,ifpresent),butneedtoperformcleanupbeforethethrownexceptioncausesexe-
cution to leave the method. Listing 3-30 provides a demonstration.
Listing 3-30. Cleaning up before handling a thrown exception
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
class Copy
{
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("usage: java Copy srcfile dst-
file");
return;
}
try
{
copy(args[0], args[1]);
}
catch (FileNotFoundException fnfe)
{
String msg = args[0]+" could not be found or might
be a directory,"+
" or "+args[1]+" could not be cre-
ated, "+
"possibly because "+args[1]+" is a
directory";
System.err.println(msg);
 
Search WWH ::




Custom Search