Uncatchable exception from gson.fromJson method?

1,356 views
Skip to first unread message

Peter

unread,
Jun 27, 2011, 9:30:39 PM6/27/11
to google-gson
I'm trying to go from a json file to a java object.

I've got this code.

try {
JsonReader reader = new JsonReader(new
InputStreamReader(inputStream, "UTF-8"));
allowedBrowsersConfiguration = gson.fromJson(reader,
AllowedBrowsersConfiguration.class);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new ServletException(e.getMessage(), e);
}

It's blowing up on the allowedBrowserConfiguration line but never gets
into the catch block.

In the IDE if I step through I get.

No such instance method: 'java.lang.Object
com.google.gson.Gson.fromJson (com.google.gson.stream.JsonReader,
java.lang.reflect.Type)

I'm using gson version 1.6

Cheers,
Peter

Peter

unread,
Jun 27, 2011, 9:33:56 PM6/27/11
to google-gson
I'm still confused that there is such a thing as an uncatchable error.

I've updated to 1.7.1 and the error has gone away though...

Cheers,
Peter

Joel

unread,
Jun 27, 2011, 10:09:56 PM6/27/11
to google-gson
Hi Peter,
It sounds like you may have encountered a "binary incompatible"
change. Is it possible that you compiled your code with 1.7.1 and then
swapped out the jar with 1.6 (or vice-versa)?

It is not that it is "uncatchable", but rather that it was an "Error"
not an "Exception". This error is actually being thrown by the JVM and
not our library. In this case it was a probably a
"NoSuchMethodError" (http://download.oracle.com/javase/1.5.0/docs/api/
java/lang/NoSuchMethodError.html). If you were to catch Throwable then
you would have caught the error above. That said, please be careful if
you attempt to catch things like "Throwable" because Errors are thrown
for good reason (such as OutOfMemoryError). In general, catching
"Throwable" is a very bad idea (please do not do this unless you
really know what you are doing and understand the consequences).

Hope this explanation makes sense.

Joel

Peter

unread,
Jun 27, 2011, 11:32:39 PM6/27/11
to google-gson
Hi Joel,
Thanks for the response. It does throw some extra light.

The gson version I was using was part of a vendor package. I've now
excluded their version and am using the version in the maven
repositories. They may well have done something weird when compiling.

That catch clause is in a filter init method. Filters are great at
stopping a tomcat context from starting without providing much info as
to why. I wanted to log something a bit better than 'filter failed to
start'.

Catching Throwable might have helped and is unlikely to cause
problems, however I think I'll leave it alone.

Cheers,
Peter
Reply all
Reply to author
Forward
0 new messages