- /*
- 毕老师用电脑上课
- 电脑可能出现的异常:
- 电脑蓝屏
- 电脑冒烟
- 老师的问题:
- 课时无法完成
- */
- class LanPingException extends Exception{
- LanPingException(Sting message){
- super(message);
- }
- }
- class MaoYanException extends Exception{
- MaoYanException(String message){
- super(message);
- }
- }
- class NoPlanException extends Exception{
- NoPlanException(String message){
- super(message);
- }
- }
- class Computer{
- private int state =1;
- public void run() throws LanPingException,MaoYanException{
- if(state==2){
- throw new LanPingException("电脑蓝屏了....");
- if(state==3){
- throw new MaoYanException("电脑冒烟了....");
- }
- System.out.println("电脑运行了");
- }
- public void reset(){
- state=1;
- System.out.println("电脑重启了。。。。");
- }
- }
- class Teacher{
- private String name;
- private Computer com;
- Teacher(String name){
- this.name=name;
- com=new Computer();
- }
- public void prelect(){
- try{
- com.run();
- }catch(LanPingException e){
- com.reset();
- }catch(MaoYanException e){
- throw new NoPlanException("课时无法完成:"+e.getMessage());
- }
- System.out.println("讲课中......");
- }
- }
- public class ExcepTest091202{
- public static void main(String[] args){
- Teacher t=new Teacher("老毕");
- try{
- t.prelect();
- }catch(NoPlanException e){
- System.out.println(e.toString());
- System.out.println("今天就由其他老师来讲课吧;如果其他老师没时间,那就放假吧。");
- }
- }
- }
复制代码
编译时的结果:
|
|