标签:stat 种类 nts weight str runtime dex native sys
throw new xxxException();
//try-catch 立即处理
public static void fun() {
File file = new File("C:/temp.txt");
if (!file.exists()) {
try {
throw new IOException();
} catch (IOException e) {
System.out.println("文件不存在");
}
}
}
//throws 稍后处理
public static void fun() throws IOException{
File file = new File("C:/temp.txt");
if (!file.exists()) {
throw new IOException();
}
}
public void test throws xxxException1, xxxException2, xxxException3 { ... }
public class Test {
public void f() throws MyException{
try {
FileReader reader = new FileReader("G:\\myfile\\struts.txt");
Scanner in = new Scanner(reader);
System.out.println(in.next());
} catch (FileNotFoundException e) {
//e 保存异常信息
throw new MyException("文件没有找到--01", e);
}
}
public void g() throws MyException{
try {
f();
} catch (MyException e) {
//e 保存异常信息
throw new MyException("文件没有找到--02", e);
}
}
public static void main(String[] args) {
Test t = new Test();
try {
t.g();
} catch (MyException e) {
e.printStackTrace();
}
}
}
com.test9.MyException: 文件没有找到--02
at com.test9.Test.g(Test.java:31)
at com.test9.Test.main(Test.java:38)
Caused by: com.test9.MyException: 文件没有找到--01
at com.test9.Test.f(Test.java:22)
at com.test9.Test.g(Test.java:28)
... 1 more
Caused by: java.io.FileNotFoundException: G:\myfile\struts.txt (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at com.test9.Test.f(Test.java:17)
... 2 more
com.test9.MyException: 文件没有找到--02
at com.test9.Test.g(Test.java:31)
at com.test9.Test.main(Test.java:38)
public class MyException extends Exception {
public MyException(String msg){
super(msg);
}
}
标签:stat 种类 nts weight str runtime dex native sys
原文地址:http://www.cnblogs.com/deng-cc/p/7462493.html