//自定义可能会出现的异常
class LanPingException extends Exception
{
LanPingException(String a)
{
super(a);
}
}
class MaoYanException extends Exception
{
MaoYanException(String a)
{
super(a);
}
}
class RenException extends Exception
{
RenException(String a)
{
super(a);
}
}
class HuanDianNaoException extends Exception
{
HuanDianNaoException(String a)
{
super(a);
}
}
class DianNao//定义电脑功能 将电脑可能出现的异常抛出
{
public void run()throws LanPingException,MaoYanException//声明异常
{
int a=3;//用来表示可能出现的异常
if(a==2)
throw new LanPingException("蓝屏了");//抛出异常
if(a==3)
throw new MaoYanException("死机了");
System.out.println("电脑运行");
}
public void chongQi()
{
System.out.println("电脑重启");
}
}
class Ren //定义人的功能和初始化信息 将人可能出现的异常抛出
{
private String nemo;
DianNao dn=new DianNao();
Ren(String nemo)
{
this.nemo=nemo;
}
public void xueXi()throws HuanDianNaoException,RenException
{
int a=2;//用来表示可能出现的异常
if(a==1)
throw new RenException(nemo+"生病了");
try
{
dn.run();
}
catch (LanPingException e)//接收异常信息并进行处理
{
System.out.println(e);//显示异常信息
dn.chongQi();
}
catch (MaoYanException e)//接收了处理不了的异常,抛出连锁反应异常
{
System.out.println(e);
throw new HuanDianNaoException("你该换电脑了");
}
System.out.println(nemo+"学习");
}
}
class Ba
{
public static void main(String[] a)
{
Ren r=new Ren("张三");
try
{
r.xueXi();
}
catch (RenException y)
{
System.out.println("去治疗吧 不要学习了");
}
catch(HuanDianNaoException h)
{
System.out.println(h);
System.out.println("没钱啊");
}
}
}
|
|