CheckedException(已检查异常)和捕获异常try_catch_finally的结构
package cn.com.exception; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TestException2 { public static void main(String[] args) { FileReader fr = null; try { fr = new FileReader("d:/b.txt"); System.out.println("step1"); char c = (char)fr.read(); System.out.println(c); }catch(FileNotFoundException e) { System.out.println("step2"); e.printStackTrace(); }catch(IOException e) { //子类异常需要在父类异常之前 e.printStackTrace(); }finally { System.out.println("step3"); try { fr.close(); }catch(IOException e) { e.printStackTrace(); } } } }使用Throws声明异常
package cn.com.exception; import java.io.FileReader; import java.io.IOException; public class TestException3 { public static void main(String[] args) throws IOException{ testFile(); } public static void testFile() throws IOException { FileReader f = new FileReader("d:/a.txt"); char a = (char)f.read(); System.out.println(a); f.close(); } }