工厂模式动态创建例外

jav*_*fan 8 java exception-handling factory-pattern

我创建了Exception xml并动态创建并抛出异常.

<exception-mappings>
<exception-mapping key="exceptionkey1">
    <class-name>com.package.CheckedException</class-name>
    <message>Checked Exception Message</message>
</exception-mapping>
<exception-mapping key="exceptionkey2">
    <class-name>com.package.UnCheckedException</class-name>
    <message>UnChecked Exception Message</message>
</exception-mapping>
Run Code Online (Sandbox Code Playgroud)

我根据异常键使用反射动态创建异常对象.

public static void throwException(final String key) throws CheckedException, UncheckedException {
    ExceptionMapping exceptionMapping = exceptionMappings.getExceptionMappings().get(key);
    if (exceptionMapping != null) {
        try {
            Class exceptionClass = Class.forName(exceptionMapping.getClassName());
            try {
                throw ()exceptionClass.newInstance(); // line X
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道哪一个类在X行进行类型转换,这样我就不需要使用If/else了.原因我不想使用if else,有可能在将来可能会添加新类,并且我不希望每次添加新异常时都更改此代码.

我的基本逻辑是我的服务层将抛出CheckedException或UncheckedException.如果抛出CheckedException,它将由我的Web层处理.此外,我不能抛出超级父类Exception或Throwable,因为我的web层只捕获CheckedException.如果抛出UncheckedException,它将显示异常页面.

请帮助我,因为我无法继续前进.

编辑:任何其他解决方案也被接受.

Rob*_*sen 11

那么,以科学的名义,这就是你如何做到的.我会建议这样做吗?绝不是.我自己会做这样的远程事吗?可能不是.

public class ExceptionFactory {
    public static void throwException(String className)
            throws CheckedException, UncheckedException {

        Class<?> exceptionClass;

        try {
            exceptionClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }

        try {
            if (CheckedException.class.isAssignableFrom(exceptionClass)) {
                throw exceptionClass.asSubclass(CheckedException.class)
                        .newInstance();
            } else if (UncheckedException.class
                    .isAssignableFrom(exceptionClass)) {
                throw exceptionClass.asSubclass(UncheckedException.class)
                        .newInstance();

            } else {
                throw new IllegalArgumentException(
                        "Not a valid exception type: "
                                + exceptionClass.getName());
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }

    public static void main(String... args) {
        try {
            throwException("CheckedException");
        } catch (CheckedException e) {
            System.out.println(e);
        } catch (UncheckedException e) {
            System.out.println(e);
        }
    }
}

class CheckedException extends Exception {
}

class UncheckedException extends Exception {
}
Run Code Online (Sandbox Code Playgroud)