在thinking in java 中有这样的例子:
//: c09:Cleanup.java
// Paying attention to exceptions in constructors.
import com.bruceeckel.simpletest.*;
import java.io.*; class InputFile {
private BufferedReader in;
public InputFile(String fname) throws Exception {
try {
in = new BufferedReader(new FileReader(fname));
// Other code that might throw exceptions
} catch(FileNotFoundException e) {
System.err.println("Could not open " + fname);
// Wasn't open, so don't close it
throw e;
} catch(Exception e) {
// All other exceptions must close it
try {
in.close();
} catch(IOException e2) {
System.err.println("in.close() unsuccessful");
}
throw e; // Rethrow
} finally {
// Don't close it here!!!
}
}
public String getLine() {
String s;
try {
s = in.readLine();
} catch(IOException e) {
throw new RuntimeException("readLine() failed");
}
return s;
}
public void dispose() {
try {
in.close();
System.out.println("dispose() successful");
} catch(IOException e2) {
throw new RuntimeException("in.close() failed");
}
}
} public class Cleanup {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null)
; // Perform line-by-line processing here...
in.dispose();
} catch(Exception e) {
System.err.println("Caught Exception in main");
e.printStackTrace();
}
monitor.expect(new String[] {
"dispose() successful"
});
}
} ///:~我有这么两个问题,一个是在InputFile的构造函数中有一个try 两个catch,在第一个catch中会再次抛出一个异常,我想问的是这个新抛出的异常是否是只能被上一层的catch来捕获还是第二个catch也可以捕获?
第二个问题是老说异常会向上层抛出,这个概念不太清楚,比如这个InputFile的构造函数是一层,那么它的上一层是否是main 中try {
InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null)
; // Perform line-by-line processing here...
in.dispose();
} catch(Exception e) {
System.err.println("Caught Exception in main");
e.printStackTrace();
}
这一层了?
//: c09:Cleanup.java
// Paying attention to exceptions in constructors.
import com.bruceeckel.simpletest.*;
import java.io.*; class InputFile {
private BufferedReader in;
public InputFile(String fname) throws Exception {
try {
in = new BufferedReader(new FileReader(fname));
// Other code that might throw exceptions
} catch(FileNotFoundException e) {
System.err.println("Could not open " + fname);
// Wasn't open, so don't close it
throw e;
} catch(Exception e) {
// All other exceptions must close it
try {
in.close();
} catch(IOException e2) {
System.err.println("in.close() unsuccessful");
}
throw e; // Rethrow
} finally {
// Don't close it here!!!
}
}
public String getLine() {
String s;
try {
s = in.readLine();
} catch(IOException e) {
throw new RuntimeException("readLine() failed");
}
return s;
}
public void dispose() {
try {
in.close();
System.out.println("dispose() successful");
} catch(IOException e2) {
throw new RuntimeException("in.close() failed");
}
}
} public class Cleanup {
private static Test monitor = new Test();
public static void main(String[] args) {
try {
InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null)
; // Perform line-by-line processing here...
in.dispose();
} catch(Exception e) {
System.err.println("Caught Exception in main");
e.printStackTrace();
}
monitor.expect(new String[] {
"dispose() successful"
});
}
} ///:~我有这么两个问题,一个是在InputFile的构造函数中有一个try 两个catch,在第一个catch中会再次抛出一个异常,我想问的是这个新抛出的异常是否是只能被上一层的catch来捕获还是第二个catch也可以捕获?
第二个问题是老说异常会向上层抛出,这个概念不太清楚,比如这个InputFile的构造函数是一层,那么它的上一层是否是main 中try {
InputFile in = new InputFile("Cleanup.java");
String s;
int i = 1;
while((s = in.getLine()) != null)
; // Perform line-by-line processing here...
in.dispose();
} catch(Exception e) {
System.err.println("Caught Exception in main");
e.printStackTrace();
}
这一层了?
C:\a
c:\a\b
c:\a\c如果 c:\a\b有问题,则转给 c:\a 而不是转给 c:\a\c
第一个问题是关于捕获异常的嵌套的:
try catch 是可以嵌套的,一般情况下,从里向外抛出,就是说第一个try catch先捕获,看异常类型是否匹配,匹配,就处理,如:e.printStackTrace()是最长见的处理,他帮助你打印异常栈。如果不匹配,再抛出,同样的,匹配,处理,知道最后,如果还是没有被处理,就会抛出异常到上一层。 上一层 含义是调用此函数或方法的一层。
一般情况下,main是最上一层了。
了解了上面,你就知道为什么我们写try catch时,第一层是具体的异常,比如FileNotFoundException 而最后一层是Exception(一切异常的父类),这样就可以保证异常一定可以被处理掉。学习异常:
你先自定义异常吧,使用throw等就可以了,然后你就都理解了。
第二个问题:上一层就是指调用这个方法的那层,在你这个程序中也就是你自己说的main那层了