Throwable类定义了3个异常处理方法
Strng getMessage()返回此throwable的简短描述
String toString()返回此throwable迭代纤细消息字符串。
void printStackTrace();JVM打印的异常对象,默认此方法,打印的异常信息是最全面的
public class Demo01TryCatch {
public static void main(String args[]){
try{
readFile("d://a.tx");
}catch (IOException e){
// System.out.println("catch 传递的文件不是声明txt" );
// System.out.println(e.getMessage());//文件的后缀名不对
// System.out.println(e.toString());// 重写Object类的toString java.io.IOException: 文件的后缀名不对
// System.out.println(e);//java.io.IOException: 文件的后缀名不对
e.printStackTrace();
//java.io.IOException: 文件的后缀名不对
//atcom.itheima.Demo01TryCatch.readFile(Demo01TryCatch.java:43)
// at com.itheima.Demo01TryCatch.main(Demo01TryCatch.java:32)
}
}
public static void readFile(String fileName) throws IOException{
if(!fileName.endsWith(".txt")){
throw new IOException("文件的后缀名不对");
}
System.out.println("路径没有问题,读取文件");
}
}