package exception;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
- 使用try-catch捕获处理异常
- @author
*/
public class TestCheckedException {
public static void main(String[] args) {
FileReader reader = null;
try{
reader = new FileReader("d:/mm.txt");
char c = (char)reader.read();
System.out.println(c);
}catch(FileNotFoundException e){ //子类异常在父类异常前面
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(reader != null){
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
####################################################################
package exception;
import java.io.FileReader;
import java.io.IOException;
/**
- 使用throws声明异常
- @author
*/
public class TestCheckedException2 {
public static void main(String[] args) throws IOException {
readMyFile();
}
public static void readMyFile() throws IOException{
FileReader reader = null;
reader = new FileReader("e:/a.txt");
char c = (char)reader.read();
System.out.println(c);
if(reader != null){
reader.close();
}
}
}