Java 异常应用 异常代码快的封装

/*
毕老师用电脑上课
问题涉及涉及两个对象
毕老师 电脑

分析其中的问题

比如 电脑蓝屏 冒烟了

冒烟了 不能抛给公司 需要无法上课异常 将无法上课异常抛给公司 

*/
class LanPingException extends Exception{
	LanPingException(String msg){
		super(msg);
	}
} 

class MaoYanException extends Exception{
	MaoYanException(String msg){
		super(msg);
	}
}

class NoPlanException extends Exception{
	NoPlanException(String msg){
		super(msg);
	}
}


class Teacher{
	private String name;
	private Computer comp;
	Teacher (String name){
		this.name=name;
		comp=new Computer();
	}
	public  void prelect()throws  NoPlanException{
		try{
			comp.run();
		System.out.println(name+ "讲课");

		}
		catch(LanPingException e){
			System.out.println(e.toString());
			comp.reset();
			prelect();
		}
		catch(MaoYanException e){//异常转换
			System.out.println(e.toString());			
			test();
			//可以对电脑进行维修
			//throw e;
			throw new NoPlanException("课时进度无法完成"+e.getMessage());
		}
		
	}

	public void test(){
		System.out.println("大家练习");
	}
} 
class Computer{
	private int state=2;
	public void run()throws LanPingException,MaoYanException{
		if(state==1)
			throw new LanPingException("电脑蓝屏");
		if(state==2)
			throw new MaoYanException("电脑冒烟了");
		System.out.println("Computer run");
	}
		public void reset(){
		state=0;
		System.out.println("电脑重启");
	}

}
class ExceptionText{
	public static void main(String[] args) {
		Teacher t=new Teacher("毕老师");
		try{
			t.prelect();
		}
		catch(NoPlanException e){
			System.out.println(e.toString()+"....");
			System.out.println("换人");
		}
		
	}
}
/*
异常的封装 不把异常暴露给不能处理掉它的
class NoAddExcepiton extends Excepiton{
	
}

void addData(Data d) throwS NoAddExcepiton{
	
	连接数据库
	try{
	添加数据,出现异常 SQLException();
	}
	catch(SQLException e){
		//处理代码
		throw new NoaAddException();
	}
	finally{
	关闭数据库
	}
}

*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值