day05--异常

package day05;

/*
 * 处理异常的格式3:
 *  try{
 *  }catch(...){
 * 
 *  }finally{
 *      //肯定执行的语句,一般是释放资源的语句,
 *      //注:如果try语句中包含return语句,finally语句在return之前执行
 *  }
 */

public class Demo7 {
    static class Student {
        private String name;

        public Student(String name) {
            this.name = name;
        }

        public int div(int a, int b) {
            return a / b;

        }

        public String toString() {
            return name;
        }

    }

    public static void main(String[] args) {
        Student s = new Student("张三");
        try {
            s.div(10, 0);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(s.toString());
            s = null;// s所指向的堆内存变成垃圾了,可以被gc回收
            System.out.println("回收资源...");
        }

    }
}
package day05;

/*
 * 思考1:老师用电脑上课,可能出现蓝屏、冒烟,从而使得了上课进度受影响了
 */
class LpException extends Exception {
    public LpException() {
        super("蓝屏");
    }

    public LpException(String msg) {
        super(msg);
    }
}

class MyException extends Exception {
    public MyException() {
        super("冒烟");
    }

    public MyException(String msg) {
        super(msg);
    }
}

class ProgressException extends Exception {
    public ProgressException() {
        super("影响课程进度");
    }

    public ProgressException(String msg) {
        super(msg);
    }
}

class Computer {
    int state = 1;

    public void run() throws LpException, MyException {
        if (state == 2) {
            throw new LpException("电脑蓝屏了");
        } else if (state == 3) {
            throw new MyException("电脑冒烟了");
        } else {
            System.out.println("电脑运行正常");
        }

    }

    public void reset() {
        state = 1;
        System.out.println("电脑重启中...");
    }
}

class Teacher {
    private String name;
    private Computer computer;

    public Teacher(String name, Computer computer) {
        this.name = name;
        this.computer = computer;
    }

    public void teach() throws ProgressException {
        try {
            computer.run();
        } catch (LpException e) {
            System.out.println("电脑蓝屏");
            computer.reset();
            System.out.println(name + "老师,继续上课");
        } catch (MyException e) {
            System.out.println("电脑冒烟");
            test();
            System.out.println("学生上自习");
            throw new ProgressException("上课进度受到了影响");
        }
    }

    public void test() {
        System.out.println("上自习");
    }

}

public class Demo9 {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.state = 3;

        Teacher teacher = new Teacher("张三", computer);
        try {
            teacher.teach();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值