Abnormal and abnormal chain

First, the unusual five keywords

  try....catch....finally

  throws   throw

  1, the role of the try statement is an error code may be placed in the try block  

  2, catch function is to capture an exception when an exception occurs try, the program will execute permissions to catch statement

  3, the role of the finally block regardless of whether the program is an exception, finally block will be executed, except when system.exit (1) cause the virtual machine to stop JVM

  4, throws an exception role is to declare a method which may arise on the method declaration of final

  5, throw a verb, is to throw an exception

Second, the abnormal chain

  The catch an exception, then subsequently another exception is thrown, and saves the original message down the chain is a typical process, also referred to as "abnormal chain."

  From JDK1.4, all subclasses of Throwable constructor can accept the object as a cause parameter. It is used to represent this cause the original exception, so you can put the original exception is passed to the new exception, so that even create and throw a new exception in the current location and you can track the location of the abnormal chain abnormality occurred first.

  For example, we have completed registration login abnormal, when the registration if the Username or Password is null, throw ZhuceException, login if abnormal is thrown LoginException, but when landing because the registration can not lead to success, the root cause of abnormal login or due to registration abnormal, we should not just throw LoginException exception shall also ZhuceException information is also given at the same time.

    code show as below:

ZhuceException.java

 1 public class ZhuceException extends Exception{
 2 
 3     public ZhuceException() {
 4         super();
 5         // TODO Auto-generated constructor stub
 6     }
 7 
 8     public ZhuceException(String message, Throwable cause) {
 9         super(message, cause);
10         // TODO Auto-generated constructor stub
11     }
12 
13     public ZhuceException(String message) {
14         super(message);
15         // TODO Auto-generated constructor stub
16     }
17 
18     public ZhuceException(Throwable cause) {
19         super(cause);
20         // TODO Auto-generated constructor stub
21     }
22     
23 }

LoginException.java

public class LoginException extends Exception{

    public LoginException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public LoginException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public LoginException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public LoginException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
    
}

User.java

public class User {
    String userName;
    int age;
    String password;
    String sex;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
}

Zhuce.java

public  class Zhuce { 
    String the userName; 
    int Age; 
    String password; 
    String Sex; 
    void zhuce (the User User) throws ZhuceException { 
        user.setUserName (the userName); 
        user.setPassword (password); 
        user.setAge (Age); 
        user.setSex ( Sex); 
        IF (the userName == null || password == null ) {
             the throw  new new ZhuceException ( "registration failed, user name or password is not null" ); 
        } the else { 
            System.out.println ( "registered successfully" ); 
        } 
        
    }
    public Zhuce(String userName,int age,String password,String sex){
        this.userName=userName;
        this.age=age;
        this.password=password;
        this.sex=sex;
    }
}

Login.java

public  class the Login {
     void Login (the User User, Zhuce zhuce) throws a LoginException {
         the try { 
            zhuce.zhuce (User); 
        } the catch (ZhuceException E) { 
            e.printStackTrace (); 
            the throw  new new a LoginException ( "registration user name or password null login fails " , E);      // the code to achieve the core strand exception
         } 
        
        IF (user.username ==" fellow Please stay " ) {
             IF (user.password ==" 123456 " ) { 
                System.out.println ( "login success" ); 
            } the else {
                 the throw new new a LoginException ( "incorrect password" ); 
            } 
        } the else { 
            System.out.println ( "user name input error!" ); 
        }         
    }     
}

Test01.java

public class Test01 {
    public static void main(String[] args) {
        User user=new User();
        Zhuce zhuce=new Zhuce("道友请留步",18,"123456","男");
        Login login=new Login();
        try{
            login.login(user, zhuce);
        }catch(LoginException e){
            e.printStackTrace();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/daguoshi/p/10979538.html