1. 共有方法判空问题
只要是public方法,入参一定要判空,这样其他人在使用该方法时入参错误了可以收到具体的错误信息。另外所有的对象在使用前都要记得判空,防止因为一个空指针异常,程序终止。
2. 异常块的包裹范围大
在finally块中,文件close之前一定记得判空
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
3. 异常信息的打印
异常信息不要只打印 getMessage 等,要把异常栈相关信息打印出来。