/*
毕老师用电脑上课
问题涉及涉及两个对象
毕老师 电脑
分析其中的问题
比如 电脑蓝屏 冒烟了
冒烟了 不能抛给公司 需要无法上课异常 将无法上课异常抛给公司
*/
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{
关闭数据库
}
}
*/
Java 异常应用 异常代码快的封装
最新推荐文章于 2022-04-24 09:50:49 发布