/*1.尝试读取一个文件: 这里的读取和写入直接放在try块,这样出了catch块引用自动消失,指向的对象可以被系统回收,catch块什么也不用做,这是JDK1.7之后常用的写法。(Orz其实自己也不是很懂,问了tao哥这里tao哥扩展讲的)*/ package com.zmz.exception; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; public class CheckedException { public static void main(String[] args) { try (FileReader reader = new FileReader(""); FileWriter writer = new FileWriter("")) { int c = reader.read(); System.out.println(c); // reader.close(); // writer.close(); } catch (IOException e) { } System.out.println("start"); FileReader reader = null ; try { reader = new FileReader("abc"); int c= reader.read(); System.out.println((char) c); } catch (FileNotFoundException e) { //文件不存在 // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { //在读取的过程中可能出现读取数据或者写入数据出错了(比如中途硬盘坏了) // TODO Auto-generated catch block e.printStackTrace(); }finally { // if(reader != null ) { // try { // reader.close(); // } catch (IOException e) { // reader = null; // } // System.out.println("释放资源"); // } } } } /*2.为员工设置年龄,对超出范围的处理异常:*/ package com.zmz.exception; public class Staff { private String name; private int age; public Staff(String name) { this.name = name; age = 18; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } /** * 修改员工年龄 * @param age * int 年龄大于18,小于60 * @throws TooYoungException * @throws TooOldException */ public void setAge(int age) throws TooYoungException, TooOldException { if(age < 18) { //太小 //中断了程序的正常流程,向上抛出(传递)一个自定义信号 throw new TooYoungException(age); } if(age > 60) { //太大 throw new TooOldException(age); } this.age = age; } } package com.zmz.exception; public class TooOldException extends Exception{ public TooOldException(int age) { // TODO Auto-generated constructor stub super("年龄太大" + age); } } package com.zmz.exception; public class TooYoungException extends Exception { private static final long serialVersionUID = 1L; public TooYoungException(int age) { super("年龄太小"+age); } } package com.zmz.exception; import java.util.*; public class Test { //编译型错误,强制要处理 public static void main(String[] args) { Staff s1 = new Staff("Bob"); System.out.println(s1.getName()); System.out.println(s1.getAge()); try { s1.setAge(12); System.out.println(s1.getAge()); } catch (TooYoungException e) { // TODO Auto-generated catch block resetAge(s1, e.getMessage()); // e.printStackTrace(); } catch (TooOldException e) { // TODO Auto-generated catch block resetAge(s1, e.getMessage()); // e.printStackTrace(); } } //handleXxxExpception private static void resetAge(Staff s1,String message) { System.out.println(message); Scanner sc = new Scanner(System.in); System.out.println("请输入年龄"); int age = sc.nextInt(); try { s1.setAge(age); } catch (TooYoungException e) { // TODO: handle exception resetAge(s1, e.getMessage()); }catch (TooOldException e2) { // TODO: handle exception resetAge(s1, e2.getMessage()); } } } /*3.用户信息的更新(常用设计模式): 待续。。。*/