Back and Forth between Two Kinds of Error Messages

K

kvnsmnsn

I've got a file "DefaultIntValue.java" that implements interface
<IIntValue> which is a descendant of <IValue> which requires it to
have <writeObject()> and <readObject()> methods, and four other files
that do similar things except that instead of reading and writing
<int>s they read and write <double>s, <float>s, <long>s and <String>s.

I originally defined the "DefaultIntValue.java" methods like so:

public void writeObject ( ObjectOutputStream isOut)
{
isOut.writeInt( integer);
}

public void readObject ( ObjectInputStream isIn)
{
integer = isIn.readInt();
}

But when I compiled these "DefaultIntValue.java" methods the compiler
told me "Unhandled exception type IOException" for both methods, and
got identical complaints for each of the other four files.

So I went in and changed my methods like so:

public void writeObject ( ObjectOutputStream isOut)
throws IOException
{
isOut.writeInt( integer);
}

public void readObject ( ObjectInputStream isIn)
throws IOException
{
integer = isIn.readInt();
}

and did similar changes to each of the four other files and then com-
piled, and the message the compiler gave me was, "Exception
IOException is not compatible with throws clause in
IValue.writeObject(ObjectOutputStream)" for <writeObject()> and "Ex-
ception IOException is not compatible with throws clause in
IValue.readObject(ObjectInputStream)" for <readObject()>. I got simi-
lar errors for each of the four other files.

Can anyone tell me what these compilation errors mean and what I have
to do to get these files to compile? Any feedback would be greatly
appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
K

kvnsmnsn

If it makes a difference all of this was done using Eclipse.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
J

jmcgill

and did similar changes to each of the four other files and then com-
piled, and the message the compiler gave me was, "Exception
IOException is not compatible with throws clause in
IValue.writeObject(ObjectOutputStream)"

You cannot simply add a throws clause to a method that overrides a
method in a superclass.

You need to either put a try block around your call and actually handle
the error, or else you need to add the throws clause to the superclass(es).
 
K

kvnsmnsn

jmcgill wrote:

=> and did similar changes to each of the four other files and then
com-
=> piled, and the message the compiler gave me was, "Exception
=> IOException is not compatible with throws clause in
=> IValue.writeObject(ObjectOutputStream)"
=
=You cannot simply add a throws clause to a method that overrides a
=method in a superclass.
=
=You need to either put a try block around your call and actually
=handle the error, or else you need to add the throws clause to the
=superclass(es).

Now I _really_ feel stupid. I'm sure this will fix my problem.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top