首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

错误处理有关问题,输出异常~求指导

2012-02-06 
异常处理问题,输出错误~求指导下面那段代码是照着书上敲出来的,但是输出错误,请问为什么?(我是菜鸟级,请讲

异常处理问题,输出错误~求指导
下面那段代码是照着书上敲出来的,但是输出错误,请问为什么?(我是菜鸟级,请讲容易理解,谢谢~)
class PasswordException extends Exception{
private int i=0;
public PasswordException (int i){
super();
this.i=i;
}
public String getMessage(){
if(i==0){
return "没有这个用户";
}
else if(i==1){
return "密码错误,请重新输入";
}
return " ";
}
}

public class UserLogin {
private String userName="";
private String password="";
  public void login() {
  InputStreamReader isr=new InputStreamReader(System.in);
  BufferedReader br =new BufferedReader(isr);
  System.out.println("请输入用户名:");
  try{
  userName=br.readLine();
  }
  catch(IOException e){
  }
  System.out.println("请输入密码:");
  try{
  password=br.readLine();
  }
  catch(IOException e){
  }
  }
  public void checkPassword() throws PasswordException{
  if(!userName.equals("administrator")){
  throw(new PasswordException(0));
  }
  else if(!password.equals("admin")){
  throw(new PasswordException(1));
  }
  }
   
  public static void main(String[] args) {
  UserLogin app=new UserLogin();
  app.login();
  try{
  app.checkPassword();
  System.out.println("密码已验证正确");
  }
  catch (Exception e){
  System.out.println(e.getMessage());
  }
  }
}


[解决办法]
lz在你抛出异常的时候要记得捕获它,就是在try后面还要有catch语句不要为空啊。

Java code
import java.io.*;class PasswordException extends Exception {    /**     *      */    private static final long serialVersionUID = 1L;    private int i = 0;    public PasswordException(final int i) {        super();        this.i = i;    }    public String getMessage() {        if (i == 0) {            return "没有这个用户";        } else if (i == 1) {            return "密码错误,请重新输入";        }        return " ";    }}public class UserLogin {    private String userName = "";    private String password = "";    public void login() {        InputStreamReader isr = new InputStreamReader(System.in);        BufferedReader br = new BufferedReader(isr);        System.out.println("请输入用户名:");        try {            userName = br.readLine();        } catch (IOException e) {            e.printStackTrace();        }        System.out.println("请输入密码:");        try {            password = br.readLine();        } catch (IOException e) {            e.printStackTrace();        }    }    public void checkPassword() throws PasswordException {        if (!userName.equals("administrator")) {            throw new PasswordException(0);        } else if (!password.equals("admin")) {            throw new PasswordException(1);        }else{            System.out.println("密码已验证正确");        }    }    public static void main(String[] args) {        UserLogin app = new UserLogin();        app.login();        try {            app.checkPassword();        } catch (Exception e) {            System.out.println(e.getMessage());        }    }}
[解决办法]
没错啊。
Java code
/** *  */package cn.luochengor.csdn;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** *  * @Author luochengor * @Date Nov 26, 2011 * @Email luochengor@foxmail.com */@SuppressWarnings("serial")class PasswordException extends Exception {    private int i = 0;    public PasswordException(int i) {        super();        this.i = i;    }    public String getMessage() {        if (i == 0) {            return "没有这个用户";        } else if (i == 1) {            return "密码错误,请重新输入";        }        return " ";    }}public class UserLogin {    private String userName = "";    private String password = "";    public void login() {        InputStreamReader isr = new InputStreamReader(System.in);        BufferedReader br = new BufferedReader(isr);        System.out.println("请输入用户名:");        try {            userName = br.readLine();        } catch (IOException e) {            System.out.println("输入被中断");        }        System.out.println("请输入密码:");        try {            password = br.readLine();        } catch (IOException e) {            System.out.println("输入被中断");        }    }    public void checkPassword() throws PasswordException {        if (!userName.equals("administrator")) {            throw (new PasswordException(0));        } else if (!password.equals("admin")) {            throw (new PasswordException(1));        }    }    public static void main(String[] args) {        UserLogin app = new UserLogin();        app.login();        try {            app.checkPassword();            System.out.println("密码已验证正确");        } catch (Exception e) {            System.out.println(e.getMessage());        }    }} 

热点排行