[6096] in java-interest
Re: Handling Exceptions - doubts ....
daemon@ATHENA.MIT.EDU (Walter Szewelanczyk)
Tue Mar 19 16:14:03 1996
Date: Mon, 18 Mar 1996 23:03:31 +0000
Reply-To: Java Interest <JAVA-INTEREST@JAVASOFT.COM>
From: Walter Szewelanczyk <Walter@ADCO.COM>
To: Multiple recipients of list JAVA-INTEREST
<JAVA-INTEREST@JAVASOFT.COM>
Hi Dhaval,
On Mon, 18 Mar 1996 19:19:01 +1100 Dhaval M Shah wrote :
> Hello Everybody,
>
> I am not able to compile this code, because the compiler says....
>
> -----
> >filewindow.java:71: Exception java.io.IOException must be caught, or it must be declared in the throws clause of this method.
> fh.openfiles(infilename,indirectory);
> ^
> >1 error
> -----
>
>
>
> While my program code is like this :
>
> -----
>
> class Filehandler
> {
> public void openfiles(String infile, String indir)
> throws java.io.IOException
> {
> File newfile = new File(indir, infile);
> FileInputStream fis = new FileInputStream(newfile);
> FileOutputStream fos = new FileOutputStream(newfile);
> System.out.println("First Byte read is : " + fis.read());
> }
> }
>
> public class filewindow extends frame
> {
> .....
> .....
>
> public boolean handleEvent(Event event)
> {
> ....
> ....
>
> Filehandler fh = new Filehandler();
> fh.openfiles(infilename,indirectory);
> }
>
> ....
> }
>
> -----------
>
> Can somebody point out where I am going wrong - since I have decl.
> java.io,IOException for void openfiles.
>
> Thanks
>
> Dhaval
>
The exception needs to be caught somewhere. Since you "throw" the
IOException in openfiles() then you would have to "catch" that
exception. The throws clause is used to delay the catrching of the
excpetion perhaps to make the code more manageable and/or readable
but the exception must still be dealt with.
One possibility is this:
public boolean handleEvent(Event event)
{
....
....
Filehandler fh = new Filehandler();
try
{
fh.openfiles(infilename,indirectory);
}
catch(IOException E)
{
//YOUR ERROR CODE HERE
}
}
Hope this helps
WAlt
********************************************************************************************
Walter Szewelanczyk Technical Director
Walter@adco.com NET VENTURE, Inc.
http://www.adco.com RR1 Box 1168A
(207) 737-8205 Richmond, Maine 04357
"One must stay in a very RIGID state of FLEXIBILITY"
********************************************************************************************