Different behavior in hosted vs web mode - IndexOutOfBoundsException instead of ArrayIndexOutOfBoundsException

34 views
Skip to first unread message

Tom Terrace

unread,
Sep 19, 2011, 10:24:29 AM9/19/11
to Google Web Toolkit
I'm seeing an error in web mode that's not showing up in hosted mode.
I initially posted the issue on the GWT-Platform group here:
http://groups.google.com/group/gwt-platform/browse_thread/thread/08bf21467e9f2ec0
.

The gist is that ArrayLists in java throw
ArrayIndexOutOfBoundsException for negative index values and
IndexOutOfBoundsException for index values >= the list size. In all of
GWT's compiled javascript both cases throw IndexOutOfBoundsException.
Here's the offending line in the detailed js output (it shows up in
all permutations):

(index < 0 || index >= 0) &&
java_util_AbstractList_indexOutOfBounds__IIV(index);

Here's the simplest test case I could come up with that produces it:

public void onModuleLoad() {
List<String> list = new ArrayList<String>();
// trickery so the compiler doesn't optimize anything out
Integer i = new Integer("-1");
try {
list.get(i);
} catch (ArrayIndexOutOfBoundsException e) {
Window.alert("caught!");
}
}

The workaround is trivial but I can file a bug for this if necessary.

Thanks for the help!
Tom

Thomas Broyer

unread,
Sep 19, 2011, 11:36:58 AM9/19/11
to google-we...@googlegroups.com
The JavaDoc says IndexOutOfBoundsException, so you shouldn't expect a more specific exception.

BTW, coding with exceptions is kind of an anti-pattern in JS (because it's faster to check for good values beforehand than rely on exceptions being thrown when bad values are passed in); this is kind of a shift from "standard" Java development, but it's basically the same, and even stronger, in GWT development: the Java runtime emulation does not necessarily throw exceptions; sometimes you'll have a JavaScriptException instead (this is even more true when you enable a few optimization switches, like disableClassCastException).

The rule of thumb is: do not count on exceptions being throw the same as in a Java VM. You writing Java, you're reading Java, you're using Java tools, but you're developping JavaScript running in a browser!
That's to say, it's highly likely that such a bug report would be closed as WontFix.

Tom Terrace

unread,
Sep 19, 2011, 3:38:15 PM9/19/11
to Google Web Toolkit
Thanks for the advice! I've submitted a patch for GWT-P so the code
won't rely on the exception being thrown.
Reply all
Reply to author
Forward
0 new messages