I’m using gson in my API. Gson#fromJson throws a run-time exception, namely, JsonSyntaxException.
JsonSyntaxException
This exception is raised when Gson attempts to read (or write) a malformed JSON element.
It’s expected that calling code will send my API malformed JSON. What’s the best way to handle such a situation? My current thinking would be to catch JsonSyntaxException and re-throw a custom checked exception.
Obviously this situation is common place. What is the preferred way to tackle this problem?
Your approach (about wrapping with a custom checked exception) is fine. But unless malformed input is expected and should be handled by callers (which is your case, but not necessarily in general for other readers here), using an unchecked exception is better.