關於 UnChecked IOException

關於UnCheckedIOException

從Java文件來看UnCheckedIOException的架構如下:
Class UncheckedIOException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.io.UncheckedIOException
All Implemented Interfaces:
Serializable

目前遇到的情境是當使用stream或將Json String To Class時
使用IOException會Catch不到,因為這些功能屬於RuntimeException。

Example:IOException

1
2
3
4
5
6
7
8
9
10
11
@Deprecated
public static <T> List<T> parseJsonToMapList(String jsonInput) throws IOException {

try {
return JSONUtils.readValue(jsonInput,
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Map.class));
} catch (IOException e) {
LogUtils.parseError.error("jsonInput: " + jsonInput, e);
throw e;
}
}

Example:UncheckedIOException

1
2
3
4
5
6
7
8
9
public static <T> List<T> parseJsonToMapInstanceList(String jsonInput) {
try {
return JSONUtils.readValue(jsonInput,
TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Map.class));
} catch (IOException e) {
LogUtils.parseError.error("jsonInput: " + jsonInput, e);
throw new UncheckedIOException(e);
}
}