JsonReader#beginObject
should throw checked exceptions, instead throws runtime IllegalStateException
.
#2663
Labels
Gson version
2.9.0
Java / Android version
Unsure - if this is needed please let me know & I can dig this up, though it should be of no consequence.
Used tools
Nothing of consequence to this bug.
Description
When parsing an JSON object that fails due to the value being an incorrect type, an unchecked exception is thrown.
beginObject
specifies a checked exception of typeIOException
here:gson/gson/src/main/java/com/google/gson/stream/JsonReader.java
Line 454 in 18b0892
When it fails to find an object, however, it throws an
IllegalStateException
here:gson/gson/src/main/java/com/google/gson/stream/JsonReader.java
Line 463 in 18b0892
and here:
gson/gson/src/main/java/com/google/gson/stream/JsonReader.java
Line 1744 in 18b0892
This isn't caught at compile time because
IllegalStateException
is aRuntimeException
. Note thatunexpectedTokenError
says itthrows IOException
but the IDE warns this throws is unused.Expected behavior
We expected an
IOException
to be thrown. Else, thebeginObject
method should check any other exceptions.Actual behavior
An unchecked runtime exception was thrown.
Reproduction steps
Attempt to parse a JSON payload expecting an object but finding another type.
e.g.
Exception stack trace
The text was updated successfully, but these errors were encountered: