/**
* 将JSON转为POJO
*/
public static <T> T fromJson(String json, Class<?> type){
T pojo;
try {
pojo = (T) OBJECT_MAPPER.readValue(json, type);
} catch (Exception e) {
LOGGER.error("convert JSON to POJO failure",e);
throw new RuntimeException(e);
}
return pojo;//这里因为try-catch中抛出了RuntimeException所以可以编译通过
}
在catch中再throw new RuntimeException的,是为了中断程序,因为runtime的异常会中断程序,不再运行下去
如果在catch块中不抛出RuntimeException 编译不通过,因为pojo没有初始化.
如果抛出,则异常处理有了确定的结果,要么pojo不会为空,要么程序终止。这样逻辑通,如果不抛出RuntimeException,则程序可能在pojo为空的情况下继续运行,则无意义,逻辑不通。