📄 failingconstructor22b.java
字号:
// exceptions/FailingConstructor22b.java
// TIJ4 Chapter Exceptions, Exercise 22 page 488
/* Create a class called FailingConstructor with a constructor that might fail
* partway through the construction process and throw an exception. In main(),
* write code that properly guards against this failure.
*/
// Solution modeled from examples in text:
import java.io.*;
public class FailingConstructor22b {
private BufferedReader in;
public FailingConstructor22b(String fname) throws Exception {
try {
in = new BufferedReader(new FileReader(fname));
} catch(FileNotFoundException e) {
System.out.println("Could not find file " + fname);
throw e;
} catch(Exception e) {
try {
in.close();
} catch(IOException e2) {
System.out.println("in.close() failed");
}
throw e;
}
}
public String getLine() {
String s;
try {
s = in.readLine();
} catch(IOException e) {
throw new RuntimeException("readLine() failed");
}
return s;
}
public static void main(String[] args) {
try {
FailingConstructor22b fc =
new FailingConstructor22b("AlwaysFinally.java");
try {
String s;
int i = 1;
while((s = fc.getLine()) != null) {
// code to print to new file:
// println(i + " " + s);
// i++;
}
} catch(Exception e) {
System.out.println("Exception caught in main()");
e.printStackTrace(System.err);
}
} catch(Exception e) {
System.out.println("FailingConstructor22b construction failed");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -