00001: import java.io.FileWriter; 00002: import java.io.IOException; 00003: import java.io.PrintWriter; 00004: 00005: 00006: class Test2 00007: { 00008: public static void main (String[] args) 00009: { 00010: double failureProbability = Double.parseDouble (args[0]); 00011: int numLines = 0; 00012: 00013: try { 00014: FileWriter fw = new FileWriter ("output.txt"); 00015: 00016: try { 00017: for (; numLines < 30; numLines++) { 00018: fw.write (generateLine (failureProbability) + "\n"); 00019: try { 00020: Thread.sleep (50); 00021: } catch (InterruptedException e) { 00022: // Ignore InterruptedException. 00023: } 00024: } 00025: } catch (E1 e) { 00026: System.err.println ("Caught E1 exception from generateLine"); 00027: } catch (IOException e) { 00028: System.err.println ("Unable to output to file"); 00029: } finally { 00030: try { 00031: fw.write ("Number of lines written: " + numLines + "\n"); 00032: fw.close (); 00033: } catch (IOException e) { 00034: System.err.println ("Unable to write summary entry and close file"); 00035: } 00036: } 00037: 00038: } catch (IOException e) { 00039: System.err.println ("Unable to open file"); 00040: e.printStackTrace (); 00041: } 00042: } 00043: 00044: 00045: static String generateLine (double failureProbability) 00046: throws E1 00047: { 00048: // Model failure with probability failureProbability. 00049: if (Math.random () < failureProbability) { 00050: throw new E1 (); 00051: } 00052: 00053: return "Log entry [" + System.currentTimeMillis () + "ms]"; 00054: } 00055: } 00056: