JSON I/O methods in "edu.cmu.inmind.multiuser.controller.common" throw Exception
errantlinguist opened this issue · 3 comments
The methods for handling JSON using edu.cmu.inmind.multiuser.controller.common.Utils
hide all exception info by having the signature throws Exception
, e.g. in the case of Utils.fromJson(String, Class<T>)
, it is not possible to see what sort of errors can occur and so callers can only guess what the underlying type is during runtime, by using a huge number of e instanceof <some exception type>
.
- Can you make sure you are using MUF version 3.0.15-rc1 or later? This is the current method signature for method fromJson (no throws Exception): public static T fromJson(String stringRepresentation, Class clazz). If you are using MUF version 3.0.15-rc1 or later and the method still has "throws Thowable" then you have to clean caches.
- The only error that can occur is JsonSyntaxException (so no idea what huge number of instanceof validations you had to do), however, this is now handled by method fromJson itself. Error (if any) is printed out on console.
Apologies; the method in question was in fact edu.cmu.inmind.multiuser.controller.common.Utils.fromJsonFile(String, Class<T>)
. This throws an Exception
and currently my downstream code simply guesses that it might be a FileNotFoundException
but this can change without my code breaking, thus changing runtime behavior (which is much worse than breaking compilation).
No publicly-accessible methods should ever list throws Exception
or throws Throwable
because it makes for much more work on the side of the end user. Swallowing the exceptions would be even worse, however. cf.
No more throws Exception nor Throwable are used in Utils class. This is the modified method signature:
public static T fromJsonFile(String fileName, Class clazz) throws FileNotFoundException
Use MUF version 3.0.15.2 to see this change reflected.