[5802] in java-interest

home help back first fref pref prev next nref lref last post

Re: Exception handling and throwing

daemon@ATHENA.MIT.EDU (Walter Szewelanczyk)
Tue Feb 27 03:34:08 1996

From: "Walter Szewelanczyk" <Walter@adco.com>
To: Susan McDaniel <mcdaniel@umich.edu>
Date: Tue, 27 Feb 1996 01:55:04 +0000
CC: java-interest@java.sun.com

Hi Susan,

On  Mon, 26 Feb 1996 14:19:49 -0500 (EST)     Susan McDaniel 
wrote :
> I'm trying to do file i/o and am having some trouble with exceptions. I know 
> how to use the try{}catch{}finally{} stuff (or at least I think I do). But
> the error message from the compiler says:
> 
> Exception java.io.IO Exception must be caught, or it must be declared in the 
> throws clause of this method.
> 
> What is a throws clause? How do I invoke one? The error is occuring in the 
> following line of code:
> 
> RandomAccessFile file = new RandomAccessFile(input, "rw");
>                         ^
> 
> 
> Thanks for any and all help
> 
> Susan McDaniel
> mcdaniel@umich.edu


you need to create a RandomAccessFile in a try/catch block

try
{
 RandomAccessFile file = new RandomAccessFile(input, "rw");
}
catch(IOExcpetion E)
{
// erro r code here
}


alternatively you could declare that your method "throws" an 
IOException so that you do not have to deal with it in that method 
but from where you called this method. 

class MyObject
{
  public void MyMethod() throws IOException
  {
    // do soem stuff
    RandomAccessFile file = new RandomAccessFile(input, "rw");
    // do more stuff
  }

  public void SomeOtherMethod()
  {
    try
      MyMethod();
    catch(IOException E)
    {
      //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"

********************************************************************************************
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post