Most contend that it is usually a very bad idea to have an empty catch block.

When the exception occurs, nothing happens, and the program fails for unknown reasons.

Example

The "tried our best" comments below are an example of what not to do :

import java.io.*;
import java.util.*;

public class BadEmptyCatch {

 /**
 * Provides an example of the serialization and subsequent de-serialization
 * of an object. In this case, the object is a List containing Strings.
 *
 * Also illustrates poor style of closing streams.
 */
 public static void main( String[] arguments ) {
    //create a Serializable List
    List quarks = new ArrayList();
    quarks.add("up");
    quarks.add("down");
    quarks.add("strange");
    quarks.add("charm");
    quarks.add("top");
    quarks.add("bottom");

    //serialize the List
    FileOutputStream file = null;
    ObjectOutputStream output = null;
    try{
      file = new FileOutputStream("quarks.ser");
      output = new ObjectOutputStream(file);
      output.writeObject(quarks);
    }
    catch(IOException exception){
      System.err.println(exception);
    }
    finally{
      //flush and close all streams
      try {
        if (output != null) {
          //This method writes any buffered output to the underlying
          //output stream, flushes the stream, then closes it
          output.close();
        }
      }
      catch (IOException exception ){
        //tried our best
      }
    }

    //deserialize the quarks.ser file
    FileInputStream inFile = null;
    ObjectInputStream input = null;
    try{
      inFile = new FileInputStream("quarks.ser");
      input = new ObjectInputStream (inFile);
      //deserialize the List
      ArrayList recoveredQuarks = (ArrayList)input.readObject();
      //display its data
      Iterator quarksItr = recoveredQuarks.iterator();
      while ( quarksItr.hasNext() ) {
        System.out.println( (String)quarksItr.next() );
      }
    }
    catch(IOException exception){
      System.err.println(exception);
    }
    catch (ClassNotFoundException exception){
      System.err.println(exception);
    }
    finally{
      //flush and close all streams
      try {
        if ( input != null ) {
          //closes the underlying input stream, which here is inFile
          input.close();
        }
      }
      catch (IOException exception){
        //tried our best
      }
    }
  }

} 

An improved version would be, for example, to use the JDK 1.4 logging facilities :

import java.io.*;
import java.util.*;
import java.util.logging.*;

public class ExerciseSerializable {

 public static void main(String[] aArguments) {
    //create a Serializable List
    List quarks = new ArrayList();
    quarks.add("up");
    quarks.add("down");
    quarks.add("strange");
    quarks.add("charm");
    quarks.add("top");
    quarks.add("bottom");

    //serialize the List
    //note the use of generic interface references

    //declared here only to ensure visibilty in finally clause
    ObjectOutput output = null;
    try{
      //use buffering
      OutputStream file = new FileOutputStream( "quarks.ser" );
      OutputStream buffer = new BufferedOutputStream( file );
      output = new ObjectOutputStream( buffer );
      output.writeObject(quarks);
    }
    catch(IOException ex){
      fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
    }
    finally{
      try {
        if (output != null) {
          //flush and close "output" and its underlying streams
          output.close();
        }
      }
      catch (IOException ex ){
        fLogger.log(Level.SEVERE, "Cannot close output stream.", ex);
      }
    }

    //deserialize the quarks.ser file
    //note the use of generic interface references

    //declared here only to ensure visibilty in finally clause
    ObjectInput input = null;
    try{
      //use buffering
      InputStream file = new FileInputStream( "quarks.ser" );
      InputStream buffer = new BufferedInputStream( file );
      input = new ObjectInputStream ( buffer );
      //deserialize the List
      ArrayList recoveredQuarks = (ArrayList)input.readObject();
      //display its data
      Iterator quarksItr = recoveredQuarks.iterator();
      while ( quarksItr.hasNext() ) {
        System.out.println( (String)quarksItr.next() );
      }
    }
    catch(IOException ex){
      fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
    }
    catch (ClassNotFoundException ex){
      fLogger.log(Level.SEVERE, "Unexpected class found upon input.", ex);
    }
    finally{
      try {
        if ( input != null ) {
          //close "input" and its underlying streams
          input.close();
        }
      }
      catch (IOException ex){
        fLogger.log(Level.SEVERE, "Cannot close input stream.", ex);
      }
    }
  }

  // PRIVATE //

  //Use Java's logging facilities to record exceptions.
  //The behavior of the logger can be configured through a
  //text file, or programmatically through the logging API.
  private static final Logger fLogger =
                Logger.getLogger(ExerciseSerializable.class.getPackage().getName());

} 

posted on 2005-11-12 06:02  java  阅读(396)  评论(0)    收藏  举报