Weird IOException's
Okay people I hope you can help me out.
This is my program code:
public static void main (String args[]) {
try {
FileOutputStream out = new FileOutputStream("c:\\output.txt");
ObjectOutputStream s = new ObjectOutputStream(out);
s.writeObject("Today");
Song song = new Song("lalala", "lala",1);
s.writeObject(song);
s.flush();
}
catch (Exception e) {
System.out.println("mis");
e.printStackTrace();
}
}
When I execute this program, nothing is written to the c:\\index.txt file. And when I remove the 'try' and 'catch' statements, I get the following error:
unreported exception java.io.FileNotFoundException; must be caugth or declared to be thrown. FileOutputStream out = new FileOutputStream("c:\\output.txt");
I hope someone can help me out.
By the way, I'm programming Java in Windows XP.
[917 byte] By [
PeterKoka] at [2007-9-28 3:24:47]

>
> When I execute this program, nothing is written to the
> c:\\index.txt file. And when I remove the 'try' and
> 'catch' statements, I get the following error:
>
Your code says 'output.txt' but I presume you knew that.
Instead of "catch(Exception e)" use "catch(Throwable e)"
You should close the streams...
s.close();
out.close();
> When I execute this program, nothing is written to the
> c:\\index.txt file. And when I remove the 'try' and
> 'catch' statements, I get the following error:
>
> unreported exception java.io.FileNotFoundException;
> must be caugth or declared to be thrown.
> FileOutputStream out = new
> FileOutputStream("c:\\output.txt");
this is absolutely normal --> IOException is a checked exception an you need to catch it; otherwise the compiler will produce the above error.