異常與異常鏈

1、異常的五個關鍵字

  try....catch....finallyjava

  throws   throwthis

  一、try語句的做用是將可能會出錯的代碼放入try程序塊中  spa

  二、catch 的功能是捕獲異常,當try中出現異常時,則將程序執行的權限交給catch語句code

  三、finally代碼塊的做用是不論程序是否出現異常,finally代碼塊都將執行,除非遇到system.exit(1)致使虛擬機JVM中止對象

  四、throws做用是聲明一個方法裏面可能出現的異常,放在方法聲明的最後blog

  五、throw是一個動詞,做用是拋出異常get

2、異常鏈

  把捕獲一個異常,而後接着拋出另外一個異常,並把原始信息保存下來是一種典型的鏈式處理,也被成爲"異常鏈"。虛擬機

  從JDK1.4之後,全部Throwable的子類在構造器中均可以接受一個cause對象做爲參數。這個cause就用來表示原始異常,這樣能夠把原始異常傳遞給新的異常,使得即便在當前位置建立並拋出了新的異常你也能經過這個異常鏈追蹤到異常最初發生的位置。it

  例如,咱們完成註冊登陸異常,當註冊時若Username或Password爲null,則拋出ZhuceException,登陸時若出現異常則拋出LoginException,然而當因爲註冊致使登錄沒法成功,登陸異常的根緣由仍是因爲註冊異常,咱們應當不只拋出LoginException異常,還應當將ZhuceException信息也同時給出。io

    代碼以下:

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 userName; int age; String password; String sex; void zhuce(User user) throws ZhuceException{ user.setUserName(userName); user.setPassword(password); user.setAge(age); user.setSex(sex); if(userName==null||password==null){ throw new ZhuceException("註冊失敗,用戶名或密碼不能爲null"); }else{ System.out.println("註冊成功"); } } 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 Login { void login (User user,Zhuce zhuce) throws LoginException{ try{ zhuce.zhuce(user); }catch(ZhuceException e){ e.printStackTrace(); throw new LoginException("註冊時用戶名或密碼爲null致使登陸失敗",e);      //實現異常鏈的核心代碼  } if(user.userName=="道友請留步"){ if(user.password=="123456"){ System.out.println("登錄成功"); }else{ throw new LoginException("密碼輸入有誤"); } }else{ System.out.println("用戶名輸入錯誤!"); } } }

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(); } } }
相關文章
相關標籤/搜索