对于 Throwadble,我们只需要简单的了解下它的几个方法即可
1.方法
1.public String getMessage : 获得异常消息字符串
2.public String toString() : 获得异常对象名和异常消息
3.public void printStrackTrace() : 打印最详细的错误信息
2.例子
public static void main(String[] args) {
try {
check("d:\\b.txt");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}
}
public static void check(String fileName) throws FileNotFoundException {
if(!fileName.equals("d:\\a.txt")) {
throw new FileNotFoundException("文件位置不存在");
}
File f = new File(fileName);
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
对于输出结果:
文件位置不存在
//getMessage
java.io.FileNotFoundException: 文件位置不存在
//toString
java.io.FileNotFoundException: 文件位置不存在
//printStackTrace
at test.Test.check(Test.java:22)
at test.Test.main(Test.java:13)