java异常小练习

/**
 * Created by patkritLee on 2016/12/18.
 * 毕老师用电脑上课
 * 开始思考上课中出现的问题。
 * 比如问题会是
 * 1.电脑蓝屏
 * 2.电脑冒烟
 * 我们需要对问题进行描述,封装成对象
 *可是当冒烟发生后,会出现讲课无法继续
 * 出现了讲师的问题:课时计划无法完成
 * NoPlanException 可以进行处理
 */
public class TestException01 {
    class LanPingException extends Exception{
        LanPingException(String message){
            super(message);
        }
    }
    class MaoYanException extends Exception{
        MaoYanException(String message){
            super(message);
        }
    }
    class NoPlanException extends Exception{
        NoPlanException(String message){
            super(message);
        }
    }
    class Computer{
        private int state = 3;
        public void run() throws LanPingException,MaoYanException {
            if(state == 2)
                throw new LanPingException("蓝屏了");
            if(state == 3)
                throw new MaoYanException("冒烟了");
            System.out.println("电脑运行");
        }
        public void reset(){
            state = 1;
            System.out.println("电脑重启");
        }
    }
    class Teacher{
        private String name;
        private Computer cmpt;
        Teacher(String name){
            this.name = name;
            cmpt = new Computer();
        }

        public void prelect() throws NoPlanException{
            try{
                cmpt.run();
            }
            catch(LanPingException e){
                cmpt.reset();
            }
            catch(MaoYanException e){
                test();
                throw new NoPlanException("课时无法继续:"+e.getMessage());
            }
            System.out.println("讲课啦");
         }
         public void test(){
            System.out.print("练习");
         }
    }
    public static void main(String[] args){
        TestException01 tep = new TestException01();
        TestException01.Teacher t = tep.new Teacher("毕老师");
        try{
            t.prelect();
        }
        catch (NoPlanException e){
            System.out.println(e.toString());
            System.out.println("换老师,或者换电脑");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值