Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7051

Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound

From KevinSimonson <kvnsmnsn@hotmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound
Date 2011-08-12 11:19 -0700
Organization http://groups.google.com
Message-ID <ec849d69-925d-4036-bf2a-be97b66da056@j37g2000prh.googlegroups.com> (permalink)
References <0f7fb5c4-6031-4778-9ef7-de757bbdda39@m5g2000prh.googlegroups.com> <j23iht$2jl$1@dont-email.me>

Show all headers | View raw


On Aug 12, 9:57 am, markspace <-@.> wrote:
> On 8/12/2011 8:39 AM, KevinSimonson wrote:
>
> > I would appreciate any information anyone can give me on this.
>
> I'm with Knute here:  tl;dr, Show us the code!

I've actually written a number of different programs trying to find
something that I can run that will throw an exception that is a
<IOException> but not a <FileNotFoundException>.  I've tried to open a
file for input when I don't have read access to the file.  I've tried
to open a file for output when the file exists and I don't have write
access to the file.  I've opened a file for input, done a "chmod u-r
<filename>" while the program's running, and then tried to close the
file.  I've opened a file for output, done a "chmod u-w <filename>"
while the program's running, and then tried to close the file.  Do any
of you want me to dig up some of those files?

Most recently I tried writing a Java program that opens a file but
then closes it without writing anything to it, that results in an
empty file, and then opens the same file and tries to read a line from
it.  But that results in a <NoSuchElementException> getting thrown,
not an <IOException>.  I don't think that file will help anyone.

Perhaps the best bet would be one I wrote back on 8 August:

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class Cf
{
  private static void prompt ( BufferedReader userInput
                             ,         String prmpt)
  {
    System.out.print( prmpt + "? ");
    try
    { userInput.readLine();
    }
    catch (IOException excptn)
    {
    }
  }

  public static void main ( String[] arguments)
  {
    if (arguments.length == 2)
    { int lineCount   = 0;
      boolean srcOpnd = false;
      try
      { BufferedReader usrInpt
                     = new BufferedReader( new
InputStreamReader( System.in));
        prompt( usrInpt, "Construct source");
        Scanner source = new Scanner( new File( arguments[ 0]));
        srcOpnd        = true;
        prompt( usrInpt, "Construct destination");
        PrintWriter dstntn
                  = new PrintWriter
                      ( new BufferedWriter( new
FileWriter( arguments[ 1])));
        String textLine;
        for (;;)
        { prompt( usrInpt, "Call <hasNextLine()>");
          if (! source.hasNextLine())
          { break;
          }
          prompt( usrInpt, "Read line");
          textLine = source.nextLine();
          prompt( usrInpt, "Write line");
          dstntn.println( textLine);
          lineCount++;
        }
        prompt( usrInpt, "Close files");
        dstntn.close();
        source.close();
        prompt( usrInpt, "All done");
      }
      catch (FileNotFoundException excptn)
      { System.out.println();
        System.out.println( "Couldn't open file!");
        System.out.println( "<srcOpnd> == " + srcOpnd + '.');
      }
      catch (IOException excptn)
      { System.out.println();
        System.out.println( "I/O problem with file!");
        System.out.println( "<srcOpnd> == " + srcOpnd + '.');
      }
    }
    else
    { System.out.println( "Usage is\n  java Cf <source>
<destination>");
    }
  }
}

Functionally, it copies the contents of a file to another file, but it
stops at every significant part of the process and prompts for user
input.  It doesn't _require any information_ from the user; all the
user has to do is hit the <Enter> key; all it does is let the user
delay execution of certain parts of the program until s/he has changed
permissions on the original file or the copy if s/he chooses to do so.

So try running this program and doing various things to the two
files.  If you can find something that actually generates a
<IOException>, please, _please_, let me know what you did!

Kevin Simonson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound KevinSimonson <kvnsmnsn@hotmail.com> - 2011-08-12 08:39 -0700
  Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound Knute Johnson <september@knutejohnson.com> - 2011-08-12 08:54 -0700
  Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound markspace <-@.> - 2011-08-12 08:57 -0700
    Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound KevinSimonson <kvnsmnsn@hotmail.com> - 2011-08-12 11:19 -0700
      Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound markspace <-@.> - 2011-08-12 12:08 -0700
        Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound Patricia Shanahan <pats@acm.org> - 2011-08-12 12:26 -0700
      Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 19:35 +0000
  Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound Patricia Shanahan <pats@acm.org> - 2011-08-12 12:31 -0700
    Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound KevinSimonson <kvnsmnsn@hotmail.com> - 2011-08-12 13:10 -0700
  Re: Trying to Understand Purpose of a Catch Block for IOException in the Presence of One for FileNotFound Lew <lewbloch@gmail.com> - 2011-08-12 14:27 -0700

csiph-web