Throwable類中3個異常處理的方法
String getMessage() 返回此throwable的簡短描述
String toString() 返回此throwable的詳細消息字符串
void printStackTrace() JVM打印對象,默認此方法,異常信息是最全面的
public class DemoTryCatch {
public static void main(String[] args) {
try {
readFile("c://a.tt");
} catch (IOException e) {
// e.printStackTrace();
// System.out.println("傳遞的文件後綴名不是txt");
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
System.out.println("後續代碼");
}
public static void readFile(String fileName) throws IOException{
if(!fileName.endsWith(".txt")){
throw new IOException("文件的後綴名不對");
}
}
}