package xtx; import java.io.FileReader; public class TryCatch { public static void main(String[] args)throws Exception { //同理向上抛出throws Exception readFile("d:/a.txt"); } public static void readFile(String path) throws Exception{ //在不想处理异常时向上抛出,throws谁调用谁处理 FileReader reader = null; try{ reader = new FileReader("d:/a.txt"); char c1 = (char) reader.read(); char c2 = (char) reader.read(); System.out.println(""+c1+c2); //在声明异常中可以不需要catch,但必须要有finally //用来关闭上面申请的资源 } finally { try { if (reader !=null){ reader.close(); } }catch (Exception e){ e.printStackTrace(); } } } }
感觉有点懵,不理解try-catch-finally的作用