加载中...

try-catch

  import java.io.FileReader;
  import java.io.IOException;
  import java.io.FileNotFoundException;
  /**
  异常的处理方法之一:try-catch
  */
  public class CheckException {
  public static void main(String[] args) {
      FileReader reader = null;
      try {
      reader = new FileReader("d:/a.txt");
      
      char c1 = (char)reader.read();
      System.out.println(c1);
      
      } catch (FileNotFoundException e) {   //子类异常放在父类异常的前面
          e.printStackTrace();    //打印异常信息
      } catch (IOException e) {     
          e.printStackTrace();
      }finally {
          try {
              if(reader!=null) {
                  reader.close();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
}
posted @ 2021-08-14 10:48  nongeason  阅读(40)  评论(0)    收藏  举报