I don't want toJson to throw an Exception, but the finally block might throw it.
Would it be preferable to wrap the bas.close() in a try/catch block or should this be re-written using try-with-resources
public String toJson() {
String json = null;
ByteArrayOutputStream bas = null;
try {
bas = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(bas, this);
json = new String(bas.toByteArray());
} catch (Exception e) {
log.trace("Unable to parse JSON", e);
} finally {
if (bas != null) {
bas.close();
}
}
return json;
}