• 代码片--写一个异常


    package com.dreamy.day04;
    
    /**
     * @author dreamy
     * 需求:老师用电脑上课
     *     开始思考上课中出现的问题。
     *     比如问题是:
     *          电脑蓝屏
     *          电脑冒烟
     *     要对问题进行描述,封装成对象。
     *     
     *     可是当冒烟发生后,出现讲课进度无法继续。
     *     出现了讲师的问题,课时计划无法完成。
     */
    class LanPingException extends Exception{
        LanPingException(String message) {
            super(message);
        }
    }
    class MaoYanException extends Exception{
        MaoYanException(String message) {
            super(message);
        }
    }
    class NoPlanException extends Exception{
        public 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;
        public Teacher(String name) {
            this.name=name;
            cmpt=new Computer();
        }
        public void prelect() throws NoPlanException{
            
            try {
                cmpt.run();
            } catch (LanPingException e) {
                System.out.println("蓝屏电脑可以重启");
                cmpt.reset();
            } catch (MaoYanException e) {
                System.out.println("冒烟电脑处理不了..");
                test();
                throw new NoPlanException("课时无法继续"+e.getMessage());
                //test();
            }
            System.out.println("讲课");
        }
        public void test() {
            System.out.println("做练习");
        }
    }
    public class ExceptionTest {
        
        public static void main(String[] args) {
            Teacher t=new Teacher("毕老师");
            try {
                t.prelect();
            }catch(NoPlanException e){
                System.out.println(e.toString());
                System.out.println("换老师或者放假");
            }
            
        }
    
    }
  • 相关阅读:
    Log4J日志配置详解
    Linux:ssh端口转发详解
    Android数据存储五种方式总结
    Nginx学习记录
    简单通用JDBC辅助类封装
    PBR.理论
    PBR.工作流贴图
    不设置readable,读取图片数据
    直接打开指定的目录
    Compute Shader基础
  • 原文地址:https://www.cnblogs.com/zhaohuan1996/p/8045096.html
Copyright © 2020-2023  润新知