创建 Java 异常不仅仅是为了打印消息



我现在正在阅读有效的Java异常文章

http://www.oracle.com/technetwork/java/effective-exceptions-092345.html

我在文章的第三页找到了这一段

不要忘记您的例外是完整的 Java 类型,它们可以 适应专门的领域、方法,甚至是构造函数 可以根据您的独特目的进行塑造。例如, 资金不足虚构抛出的异常类型 CheckingAccount processCheck(( 方法可以包括一个 透支保护对象,能够转移所需的资金 弥补另一个帐户的不足,该帐户的身份取决于如何 支票账户已设置。

如果我在线检查,我发现自定义异常代码如下所示

public class DivisorCannotbeZeroException extends RuntimeException {

    private static final long serialVersionUID = 1L;
    public DivisorCannotbeZeroException(){
        super();
        System.out.println("I am doing something more");
    }
    public DivisorCannotbeZeroException(String message){
        super(message);
    }
}

甚至 print 语句在该代码中也不起作用。 您能否解释一下,有关如何向特定于我们要求的自定义异常类添加更多功能?

您必须构造该Exception的实例(使用默认构造函数(才能调用该构造函数(通常通过throw它(,例如

public static void main(String[] args) {
    throw new DivisorCannotbeZeroException();
}

输出为

I am doing something more
Exception in thread "main" com.stackoverflow.Example
    at com.stackoverflow.Example.main(Example.java:18)

MyException.java (User-Defined(

  package com.journaldev.exceptions;
  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;
        } 
    }

自定义异常示例

    package com.journaldev.exceptions;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    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");
                }
            }
        }
    }

可以为任何其他类执行例外,可以继承特定进程的属性

最新更新