Java 电脑蓝屏冒烟异常处理

class LanPingException extends Exception
{
LanPingException(){
super();
}

LanPingException(String message){
super(message);
}
}

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

class ClassProgressException extends Exception
{
ClassProgressException(){
super();
}

ClassProgressException(String message)
{
super(message);
}
}

class Computer
{
private int state=3;
Computer(){}

public void run()throws LanPingException,MaoYanException
{
   if(state==2){
       throw new LanPingException("蓝屏了");
   }else if(state==3)
	   throw new MaoYanException("冒烟了");
   else
       System.out.println("电脑运行");
}
public void reset()
{
   System.out.println("电脑重启");
}

}

class Teacher
{
private String name;
private Computer computer;

Teacher(){}

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

public String getName(){
  return name;
}

//上课
public void teach()throws ClassProgressException
{
	try{
        System.out.println(name+"上课");
        computer.run();

	}catch(LanPingException e){
        System.out.println(e.getMessage());
	    computer.reset();
		System.out.println("继续上课");

	}catch(MaoYanException ee){
	    System.out.println(ee.getMessage());
		throw new ClassProgressException("上课进度异常");
	}
}

}

class Demo1
{
public static void main(String[] args)
{
//1.老师用电脑上课,用面向对象的思想实现
//可能发生的异常有电脑蓝屏,电脑冒烟,电脑冒烟会影响讲课进度,引发讲课进度异常.
try{
Teacher t=new Teacher(“杨老师”);
t.teach();

	 }catch(ClassProgressException e){
	     System.out.println(e.getMessage());
		 System.out.println("老师休息,学生练习");
	 }
	 
	
}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值