Abnormal exercise

com.qf.test Package Penalty for;
 / * 
 * Use custom exception solve 
 * 
 * Title: teachers use computers in class 
 * computer blue screen may occur when classes restart the computer abnormal --- 
 * crash abnormal abnormal ---- ---- teacher in the class - go to the Apple store to repair the computer 
 * 
 * Note: when using trycatch appropriate? 
 * use appropriate when the method invocation exception may occur 
 * 
 * / 
public  class Demo10 {
     public  static  void main (String [] args) { 
        Teacher1 teacher1 = new new Teacher1 ( new new Computer ());
         the try { 
            teacher1.work (); 
        } the catch (ClassingException E) {  
            e.printStackTrace ();
            the System. OUT.println("去中关村修电脑");
        }
    }
}

//创建三个异常类
class ClassingException extends Exception{
    public ClassingException() {}

    public ClassingException(String message) {
        super(message);
    }
}

class LanPingException extends Exception{
    public LanPingException() {
    }

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

class MaoYanException extends Exception{
    public MaoYanException() {
    }

    public MaoYanException(String message) {
        super(message);
    }
}

class Teacher1 {
    Computer computer;

    public Teacher1(Computer computer) {
        super();
        this.computer = computer;
    }
    //老师上课
    public void work() throws ClassingException
    {
        try {
            computer.computerWork();
        } catch (LanPingException  e) {
            e.printStackTrace();
            computer.reset();
        } catch(MaoYanException E) { 
            e.printStackTrace (); 
            the throw  new new ClassingException ( " teacher class can not continue " ); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        
    } 
} 

class Computer {
     // set the current state value : 1. abnormal representative of blue smoke to occur 2. 
    int Status = 2 ;
     // restarting method 
    public  void RESET () { 
        the System. OUT .println ( " restart the computer " ;) 
    } 
    
    // computer work 
    public void computerWork () throws LanPingException, MaoYanException 
    { 
        Switch (Status) {
         Case  . 1 : // blue 
            the throw  new new LanPingException ( " computer blue screen " );
             // BREAK; 
        Case  2 : // smoke 
            the throw  new new MaoYanException ( " PC smoking a " );
             // BREAK; 
        default :
             BREAK ; 
        } 
    } 
} 
// used in the process of rewriting the attention point exception: 
/ *
* A method for abnormality by the same name subclass declared to = <parent class 
* 2. The same method on the subclass declared abnormality may be inconsistent with the parent class, may not be unusual statement. Subclass but this time the method can not throw exceptions 
* 3. If the same method on the subclass exception statement, the parent must be declared exception. 
* / 
class BadComputer the extends Computer { 
    
    public  void computerWork () 
    { 
        
    } 
}

 

Guess you like

Origin www.cnblogs.com/yumengfei/p/10960377.html