如何用用户定义的字段编写自定义异常?

时间:2014-03-21 08:29:07

标签: java exception exception-handling

这可能是一件容易的事,但我只是有点困惑。如何使用用户定义的字段编写自定义异常。

让我们举一个例子:

public class MyException extends Exception
{
public  MyException()
    {
        super();
    }

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

    public MyException(String message, Throwable cause){
        super(message,cause);
    }
}

现在我想要这样的东西:

public MyException(String errCode, String message, Throwable cause){
        //Want to get same result as other constructor but with errCode field   
}

只是混淆了如何做到这一点。请帮忙!

5 个答案:

答案 0 :(得分:9)

public class MyException extends Exception {
    private String errCode;

    public MyException(String errCode, String message, Throwable cause) {
        super(message, cause);
        this.errCode = errCode;
    }

    //getter, setter

}

答案 1 :(得分:2)

您需要将类中的错误代码保存为成员变量,然后在其上提供getter方法。

private String errCode;

public MyException(String errCode, String message, Throwable cause){

      super(message, cause);  
      this.errCode = errCode
}

public String getErrCode() {
   return this.errCode;
 }

当您收到异常对象时,可以调用getErrCode方法来获取错误代码

答案 2 :(得分:1)

public class MyException extends Exception { 
    private String errCode;

public  MyException(){
    super();
}

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

public MyException(String message, Throwable cause){
    super(message,cause);
}

public MyException(String errCode, String message, Throwable cause){
        super(message,cause);
        this.errCode = errCode;  
}
public String getErrCode() {
   return this.errCode;
 }
 }

答案 3 :(得分:1)

有很多方法可以实现这一点,例如

public static void main(String[] args) throws FileNotFoundException, IOException {
        try{
            testException(-5);
            testException(-10);
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            System.out.println("Releasing resources");          
        }
        testException(15);
    }

    public static void testException(int i) throws FileNotFoundException, IOException{
        if(i < 0){
            FileNotFoundException myException = new FileNotFoundException("Negative Integer "+i);
            throw myException;
        }else if(i > 10){
            throw new IOException("Only supported for index 0 to 10");
        }

    }

或者通过创建自定义类

public class MyException extends Exception {

    private static final long serialVersionUID = 4664456874499611218L;

    private String errorCode="Unknown_Exception";

    public MyException(String message, String errorCode){
        super(message);
        this.errorCode=errorCode;
    }

    public String getErrorCode(){
        return this.errorCode;
    }


}

然后你可以像

一样使用它
public class CustomExceptionExample {

    public static void main(String[] args) throws MyException {
        try {
            processFile("file.txt");
        } catch (MyException e) {
            processErrorCodes(e);
        }

    }

    private static void processErrorCodes(MyException e) throws MyException {
        switch(e.getErrorCode()){
        case "BAD_FILE_TYPE":
            System.out.println("Bad File Type, notify user");
            throw e;
        case "FILE_NOT_FOUND_EXCEPTION":
            System.out.println("File Not Found, notify user");
            throw e;
        case "FILE_CLOSE_EXCEPTION":
            System.out.println("File Close failed, just log it.");
            break;
        default:
            System.out.println("Unknown exception occured, lets log it for further debugging."+e.getMessage());
            e.printStackTrace();
        }
    }

    private static void processFile(String file) throws MyException {       
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new MyException(e.getMessage(),"FILE_NOT_FOUND_EXCEPTION");
        }finally{
            try {
                if(fis !=null)fis.close();
            } catch (IOException e) {
                throw new MyException(e.getMessage(),"FILE_CLOSE_EXCEPTION");
            }
        }
    }

}

答案 4 :(得分:0)

该提交的文件应该是最终的。这是正确的做法。如下所示

public class MyException extends Exception {

private final int status;

public MyException(String message, int status) {
    super(message);
    this.status = status;
}

public int getStatus() {
    return status;
}

}