异常的输出
//得到异常信息
private static String sysOutExcepMsg(java.lang.Exception e){
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
return str;
}
Multiple catch
try (BufferedReader br = new BufferedReader(…);
BufferedWriter writer = new BufferedWriter(…)) {// Try-with-resources
// do something
catch ( IOException | XEception e) {// Multiple catch
// Handle it
}
try(...)会自动释放资源
Resource resource = resourceLoader.getResource("classpath:/myfile/test.xml");
try (
InputStreamReader reader = new InputStreamReader(resource.getInputStream());
BufferedReader br = new BufferedReader(reader);
) {
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str).append("\n");
}
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
异常的检查的工具类
Objects.requireNonNull(obj);
Objects.isNull(obj);
Objects.nonNull(obj);