异常与异常链
一、异常的五个关键字
try....catch....finally
throws throw
1、try语句的作用是将可能会出错的代码放入try程序块中
2、catch 的功能是捕获异常,当try中出现异常时,则将程序执行的权限交给catch语句
3、finally代码块的作用是不论程序是否出现异常,finally代码块都将执行,除非遇到system.exit(1)导致虚拟机JVM停止
4、throws作用是声明一个方法里面可能出现的异常,放在方法声明的最后
5、throw是一个动词,作用是抛出异常
二、异常链
把捕获一个异常,然后接着抛出另一个异常,并把原始信息保存下来是一种典型的链式处理,也被成为"异常链"。
从JDK1.4以后,所有Throwable的子类在构造器中都可以接受一个cause对象作为参数。这个cause就用来表示原始异常,这样可以把原始异常传递给新的异常,使得即使在当前位置创建并抛出了新的异常你也能通过这个异常链追踪到异常最初发生的位置。
例如,我们完成注册登录异常,当注册时若Username或Password为null,则抛出ZhuceException,登录时若出现异常则抛出LoginException,然而当由于注册导致登陆无法成功,登录异常的根原因还是由于注册异常,我们应当不仅抛出LoginException异常,还应当将ZhuceException信息也同时给出。
代码如下:
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(); } } }