Home Segments Top Top Previous Next

614: Mainline

If a program fails to find a file, the reason may be that the file has not yet appeared, and the right approach to take is to try again. For example, the following variation on the program shown in Segment 613 loops if the file does not exist, because the catch expression continues to reset the tryAgain variable to true:

import java.io.*;
import java.util.*;
public class Auxiliaries {
 public static Vector readMovieFile(String fileName) {
  boolean tryAgain = true;              
  Vector v = new Vector();
  while (tryAgain) {                    
   try {
    tryAgain = false;                   
    // Rest of try expression as in Segment 613
   }
   catch (FileNotFoundException e) {            
    tryAgain = true;                            
   }                                            
   catch (IOException e) { 
    System.out.println(e); 
   } 
  } 
  return v; 
 } 
}