Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Trying to Understand Purpose of a Catch Block for IOException in thePresence of One for FileNotFound

Reply
Thread Tools

Trying to Understand Purpose of a Catch Block for IOException in thePresence of One for FileNotFound

 
 
KevinSimonson
Guest
Posts: n/a
 
      08-12-2011
If I write a piece of code that constructs an object of class
<PrintWriter> by passing it an object of class <BufferedWriter>,
constructed by passing _it_ an object of type <FileWriter> (all three
classes under <java.io>), don't have a <throws> clause, have only a
<catch> clause for <FileNotFoundException>, and then try to compile
the code the compiler complains, telling me, "unreported exception
java.io.IOException java.io.IOException: must be caught or declared to
be thrown". Of course, if I put a <catch> clause there for
<IOException>, that fixes the problem and the program compiles just
fine.

If, on the other hand, I write a piece of code that constructs an
object of class <Scanner>, constructed by passing it an object of type
<File>, don't have a <throws> clause for <FileNotFoundException>, and
don't put the constructor call in a <try> block at all, I get a
similar complaint about me not saying anything about exception
<FileNotFound>.

Finally, if I have some code that has constructors for _both
<PrintWriter> and_ <Scanner>, I have to have a <catch> clause for
_both <IOException> and_ <FileNotFoundException>, in order to keep the
compiler happy. But in such a situation I have not been able to find
a way to _actually get <IOException> thrown_! What is the purpose of
having a <catch> block for <IOException> if nothing I do with my code
will actually throw an <IOException> that is not a
<FileNotFoundException>? Or, alternately, is there something I can do
to _get_ an <IOException> that is not a <FileNotFoundException>
thrown?

I would appreciate any information anyone can give me on this.

Kevin Simonson
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      08-12-2011
On 8/12/2011 8:39 AM, KevinSimonson wrote:
> If I write a piece of code that constructs an object of class
> <PrintWriter> by passing it an object of class<BufferedWriter>,
> constructed by passing _it_ an object of type<FileWriter> (all three
> classes under<java.io>), don't have a<throws> clause, have only a
> <catch> clause for<FileNotFoundException>, and then try to compile
> the code the compiler complains, telling me, "unreported exception
> java.io.IOException java.io.IOException: must be caught or declared to
> be thrown". Of course, if I put a<catch> clause there for
> <IOException>, that fixes the problem and the program compiles just
> fine.
>
> If, on the other hand, I write a piece of code that constructs an
> object of class<Scanner>, constructed by passing it an object of type
> <File>, don't have a<throws> clause for<FileNotFoundException>, and
> don't put the constructor call in a<try> block at all, I get a
> similar complaint about me not saying anything about exception
> <FileNotFound>.
>
> Finally, if I have some code that has constructors for _both
> <PrintWriter> and_<Scanner>, I have to have a<catch> clause for
> _both<IOException> and_<FileNotFoundException>, in order to keep the
> compiler happy. But in such a situation I have not been able to find
> a way to _actually get<IOException> thrown_! What is the purpose of
> having a<catch> block for<IOException> if nothing I do with my code
> will actually throw an<IOException> that is not a
> <FileNotFoundException>? Or, alternately, is there something I can do
> to _get_ an<IOException> that is not a<FileNotFoundException>
> thrown?
>
> I would appreciate any information anyone can give me on this.
>
> Kevin Simonson


Show us the actual code.

--

Knute Johnson
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-12-2011
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!


 
Reply With Quote
 
KevinSimonson
Guest
Posts: n/a
 
      08-12-2011
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
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      08-12-2011
On 8/12/2011 11:19 AM, KevinSimonson wrote:
> new FileWriter( arguments[ 1])));


> 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!



The FileWriter constructor is declared to generate an IOExcpetion. I
don't have time to mess with it, but it's likely a large variety of
network file systems could generate an IO error of various types.

While any other error might be swallowed by the PrintWriter, you are
briefly exposed to the vagaries file system while that constructor is
running.



 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      08-12-2011
KevinSimonson <> wrote:
> 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>.


Just speculating: have you tried to read a large file from
an USB thumbdrive and midway disconnect the drive?

Warning: that *may* damage the thumbdrive (even physically,
not just the filesystem on it!), so use an old small one that
you wont miss in case it's really broken afterwards.

> I've opened a file for input, done a "chmod u-r <filename>"
> while the program's running, ...


Removing permissions after the file has been opened doesn't
ever matter, anyway. Permissions are only checked at open().

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      08-12-2011
KevinSimonson <> writes:
>public static void main ( String[] arguments)

(...)
> catch (IOException excptn)

(...)
>files. If you can find something that actually generates a
><IOException>, please, _please_, let me know what you did!


I did not read all of the thread. But if you feel annoyed
having to write a catch while believing to know that there
will not be such an exception, try (no pun intended):

public static void main( final java.lang.String[] args )
throws java.io.IOException
{ ... }

 
Reply With Quote
 
KevinSimonson
Guest
Posts: n/a
 
      08-12-2011
On Aug 12, 1:31*pm, Patricia Shanahan <p...@acm.org> wrote:
> On 8/12/2011 8:39 AM, KevinSimonson wrote:
> ...> Finally, if I have some code that has constructors for _both
> > <PrintWriter> *and_<Scanner>, I have to have a<catch> *clause for
> > _both<IOException> *and_<FileNotFoundException>, in order to keep the
> > compiler happy.

>
> ...
>
> This is the most puzzling part of your question. FileNotFoundException
> is a subclass of IOException, so a catch clause for IOException should
> be sufficient.
>
> Of course, if you want to deal with FileNotFoundException separately you
> do have the option of putting in a separate catch clause for it, but the
> compiler should not be requiring both.
>
> Patricia


Patricia, you were absolutely right! Following up on your puzzlement,
I rewrote the program I posted, taking out the <import> of
<FileNotFoundException> and consolidating my <catch> blocks into:

catch (IOException excptn)
{ System.out.println();
System.out.println( "Couldn't open file!");
System.out.println( "<srcOpnd> == " + srcOpnd + '.');
}

This works just fine. All I can think of is that when I tried to
compile a program that just took input the compiler complained about
<FileNotFound> and when I tried to compile a program that just wrote
output the compiler complained about <IOException>, so maybe I just
_assumed_ that a program that both read input and wrote output would
need both exceptions!

Anyhow, thanks for pointing me in the right direction.

Kevin Simonson
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-12-2011
KevinSimonson wrote:
> If I write a piece of code that constructs an object of class
> <PrintWriter> by passing it an object of class <BufferedWriter>,
> constructed by passing _it_ an object of type <FileWriter> (all three
> classes under <java.io>), don't have a <throws> clause, have only a
> <catch> clause for <FileNotFoundException>, and then try to compile
> the code the compiler complains, telling me, "unreported exception
> java.io.IOException java.io.IOException: must be caught or declared to
> be thrown". Of course, if I put a <catch> clause there for
> <IOException>, that fixes the problem and the program compiles just
> fine.
>
> If, on the other hand, I write a piece of code that constructs an
> object of class <Scanner>, constructed by passing it an object of type
> <File>, don't have a <throws> clause for <FileNotFoundException>, and
> don't put the constructor call in a <try> block at all, I get a
> similar complaint about me not saying anything about exception
> <FileNotFound>.
>
> Finally, if I have some code that has constructors for _both
> <PrintWriter> and_ <Scanner>, I have to have a <catch> clause for
> _both <IOException> and_ <FileNotFoundException>, in order to keep the
> compiler happy. But in such a situation I have not been able to find
> a way to _actually get <IOException> thrown_! What is the purpose of
> having a <catch> block for <IOException> if nothing I do with my code
> will actually throw an <IOException> that is not a
> <FileNotFoundException>? Or, alternately, is there something I can do
> to _get_ an <IOException> that is not a <FileNotFoundException>
> thrown?


Since 'FileNotFoundException' is a subtype of 'IOException', a catch block for the latter will catch the former.

Another subtype of 'IOException' that is not a super- or subtype of 'FileNotFoundException' is 'EOFException'. You could write a loop to keep reading a file (that is found) past its end to throw 'EOFException'.

A catch block for 'IOException' will catch both 'FileNotFoundException' and 'EOFException', as well as any other subtype of 'IOException'.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IOException....try...catch... bhavish1987@gmail.com Java 3 03-27-2007 02:58 AM
System.IO.Filenotfound error niju ASP .Net 0 11-09-2005 10:58 PM
Server.MapPath FileNotFound problem Aahz ASP .Net 0 08-09-2005 08:41 AM
FileNotFound error passing UNC path into web service Jon Sims ASP .Net Web Services 0 07-19-2005 10:33 AM
Soap/FileNotFound exception Rhett Shoemaker ASP .Net Web Services 0 10-05-2004 11:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57