Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用用户定义的字段编写自定义异常?_Java_Exception_Exception Handling - Fatal编程技术网

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

Java 如何使用用户定义的字段编写自定义异常?,java,exception,exception-handling,Java,Exception,Exception Handling,这可能是件容易的事,但我只是有点困惑。如何使用用户定义的字段编写自定义异常 让我们举一个例子: public class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } public MyException(Strin

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

让我们举一个例子:

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   
}

只是不知道怎么做。请帮忙

您需要将类中的错误代码作为成员变量保存,然后给出一个getter方法

public class MyException extends Exception {
    private String errCode;

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

    //getter, setter

}
private String errCode;

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

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

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

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

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

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;
 }
 }
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 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");
            }
        }
    }

}

该文件应为最终文件。这是正确的做法。如下

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;
}

}

什么是困惑?只需编写构造函数并调用适用的超级构造函数。将errCode值保存在MyExceptionWell的一个实例变量中,
异常
是一个类似于任何其他类的类。。。您到底有什么问题?将错误代码附加到消息中,然后调用
super(消息,原因)?@sidgate没有适用的超级构造函数。@Theolodis我需要它作为单独的字段。
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;
}