stringBuffer.append(char) now throws IOException?!? HUH?!?

I recently had the 1.5 JDK shoved down my throat, and today was the first time I actually had to compile anything in it.

And I suddenly found 7 compiler errors complaining that I'm appending to a StringBuffer without dealing with IOExceptions.

Two questions:

1. Yes, I eventually figured out that StringBuffer inherits this requirement from the new "Appendable" interface, but given that a StringBuffer doesn't do I/O, WHY was it set up this way?

2. Again, given that the method is effectively lying about what it can throw, how am I to deal with it? A "try/catch" with an empty catch?

--

JHHL

[638 byte] By [hbquikcomjamesla] at [2007-11-26 16:45:50]
# 1

you can surround with try and catch, catch IOException.

print the stack trace to see where the problem is.

StringBuffer shouldn't need to be surrounded with try and catch though, if I remember correctly.

or try StringBuilder ? unless you need synchronization then StringBuffer is your only choice.

o00oo00oa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 2
> stringBuffer.append(char) now throws IOExceptionNot according to the JavaDocs. I think you're doing something else wrong.
warnerjaa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 3

> I recently had the 1.5 JDK shoved down my throat,

Shoved down your throat? Java 6 is here. Somebody's doing you a favor.

> And I suddenly found 7 compiler errors complaining

> that I'm appending to a StringBuffer without dealing

> with IOExceptions.

Not according to my javadocs.

This code didn't require a try/catch:

/**

* Created by IntelliJ IDEA.

* User: Michael

* Date: Jan 30, 2007

* Time: 8:48:57 PM

* To change this template use File | Settings | File Templates.

*/

public class AppenderCheck

{

public static void main(String[] args)

{

StringBuffer buffer = new StringBuffer(1024);

for (int i = 0; i < args.length; ++i)

{

for (int j = 0; j < args[i].length(); ++j)

{

buffer.append(args[i].charAt(j));

}

}

System.out.println(buffer);

}

}

> Two questions:

>

> 1. Yes, I eventually figured out that StringBuffer

> inherits this requirement from the new "Appendable"

> interface, but given that a StringBuffer doesn't do

> I/O, WHY was it set up this way?

Might be part of a redesign that included the new StringBuilder class. It's compatible with StringBuffer that way, but not synchronized.

> 2. Again, given that the method is effectively lying

> about what it can throw, how am I to deal with it? A

> "try/catch" with an empty catch?

My example didn't require a try/catch. Is that representative of what you're doing?

%

duffymoa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> My example didn't require a try/catch. Is that

> representative of what you're doing?

It sure doesn't look much different:

. . .

char ch = 0;

. . .

StringBuffer buf = new StringBuffer();

. . .

if (all || (index[address-1] >= 0)) buf.append(ch);

. . .

hbquikcomjamesla at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> > stringBuffer.append(char) now throws IOException

> Not according to the JavaDocs. I think you're doing something else wrong.

And yet:

append

public StringBuffer append(char c)

. . .

Specified by:

append in interface Appendable

=========

append

Appendable append(char c)

throws IOException

. . .

hbquikcomjamesla at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 6

An overriding method can declare to throw fewer exceptions than the overridden method. What those declarations say is that some implementations of the Appendable.append method may throw an IOException. The way StringBuffer.append is declared guarantees that this particular implementation can never throw such an exception.

If you are having problems with your code it would be a good idea to post it nowit has already been demonstrated that StringBuffer.append can be used without exception handling.

jsalonena at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 7
And yet I just opened a new project in JBuilder, plugged "duffymo"'s (Michael's?) example into it, and tried to compile it.It refused to compile, the same as my code, with the same error message.My JBuilder is 6.0.438.0. My JDK is 1.5.0_10.
hbquikcomjamesla at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 8
Are you sure it isn't a JBuilder issue? http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5013602Try going to your command line and compiling it with the JDK directly. Does that work?
doremifasollatidoa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 9
> Shoved down your throat? Java 6 is here. Somebody's doing you a favor.Since when is fixing something that isn't broken a favor?
hbquikcomjamesla at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 10
> > Shoved down your throat? Java 6 is here. Somebody's> doing you a favor.> > Since when is fixing something that isn't broken a> favor?It called preventative maintenance.
zadoka at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 11
try using a different ide (e.g. command line).
rkippena at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 12

> > Shoved down your throat? Java 6 is here. Somebody's

> doing you a favor.

>

> Since when is fixing something that isn't broken a

> favor?

Traditionally Sun end of life's two versions back, so I would expect that for 1.4 soon if it hasn't occurred already.

And that means that if there are problems in that VM then you will no longer get fixes from Sun. And it will no longer work on new OS versions.

jschella at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 13

> And yet I just opened a new project in JBuilder,

> plugged "duffymo"'s (Michael's?) example into it, and

> tried to compile it.

>

> It refused to compile, the same as my code, with the

> same error message.

>

> My JBuilder is 6.0.438.0. My JDK is 1.5.0_10.

IntelliJ 6.0.4 compiled and ran it without a problem, using JDK 1.5.0_04

%

duffymoa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 14
That's Sun JVM, BTW. Not JRockit.%
duffymoa at 2007-7-8 23:13:12 > top of Java-index,Java Essentials,Java Programming...
# 15
> That's Sun JVM, BTW. Not JRockit.What's a "JRockit"?
hbquikcomjamesla at 2007-7-21 16:50:09 > top of Java-index,Java Essentials,Java Programming...
# 16
> > That's Sun JVM, BTW. Not JRockit.> > What's a "JRockit"?The VM used by BEA: http://www.bea.com/framework.jsp?CNT=index.htm&FP=/content/products/jrockit/
tsitha at 2007-7-21 16:50:09 > top of Java-index,Java Essentials,Java Programming...
# 17
It's evidently my "old-and-busted" JBuilder that's the cause of all my grief. When I had the programmer at the next desk (see my "VerifyError" thread) recompile from her "new hotness" JBuilder, it wouldn't compile unless I removed the try/catches I'd put in.
hbquikcomjamesla at 2007-7-21 16:50:09 > top of Java-index,Java Essentials,Java Programming...