class lanpingException extends Exception{//因为这个自定义异常问题是可以有调用者老师解决的,那么应该停止并且去修改它,编译时异常
lanpingException(){
super();
}
lanpingException(String Message){
super(Message);
}
}
class jiaoshihuoException extends Exception{
jiaoshihuoException(){
super();
}
jiaoshihuoException(String Message){
super(Message);//把信息传递到父类
}
}
class maoyanException extends Exception{
maoyanException(){
super();
}
maoyanException(String message){
super(message);
}
}
class NoplanException extends Exception{
NoplanException(){
super();
}
NoplanException(String message){
super(message);
}
}
class NoteBook{
private int stic=2;
NoteBook(){
}
void run()throws lanpingException,maoyanException{
if(stic==1){
throw new lanpingException("电脑蓝屏了");
}if(stic==2){
throw new maoyanException("电脑冒烟");
}
else{
System.out.println("电脑正在运行");
}
}
void rerult(){
System.out.println("电脑要重启");
}
}
class Teacher{
private String name;
private NoteBook book;
private int stic=2;
//上面把教师的属性封装起来,另外book这个类是与教师捆绑在一起的,所以我就吧NoteBook类封装在教师类中。
Teacher(String name){//使用一个有参构造方法,这个方法里面因为有个NoteBook类,那么我们必须在下面的代码中new出对象
this.name=name;
book=new NoteBook();
}
public void teach()throws jiaoshihuoException,NoplanException{
try{
book.run();//book调用了方法;
}
catch(lanpingException e){
book.rerult();
}catch(maoyanException e){
huan();
throw new NoplanException("课程停止");//异常转换
}
if(stic==1){
throw new jiaoshihuoException("教室着火了");
}
else{
System.out.println(name+"老师正在教书");
}
}
public void jiejue(){
System.out.println("老师灭火");
}
public void huan(){
System.out.println("公司知道了后给换了台电脑");
}
}
public class text26{
public static void main(String[] args){
Teacher T=new Teacher("毕");
try{
T.teach();
}
catch(jiaoshihuoException e){
T.jiejue();
}catch(NoplanException e){
System.out.println(e.getMessage());
}
}
}