用于动态创建异常的工厂模式

问题描述 投票:8回答:6

我已经创建了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>

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

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

    }
}

我想知道在X行键入哪个类,这样就不需要使用If / else。我不想使用的背后原因是,将来可能会添加新的类,并且我不想每次添加新异常时都更改此代码。

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

[请帮助我,因为我无法继续进行。

编辑:任何其他解决方案也可接受。

java exception-handling factory-pattern
6个回答
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 {
}

2
投票

我看不出这家工厂的意义。即使您可以使用它(通过将所有异常作为单个祖先类的子类抛出),它的用法也将如下所示:

....
if (somethingInWrong) {
    ExceptionFactory.throwException("SomeKey");
}
....

2
投票

一些调整:

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

1
投票

无需使用反射(正如我在you shouldn't use reflection unless you really have to...上面所说的那样)。

您可以将异常类实现为如下形式:


1
投票

这是我参加这场德比大赛的机会。 :-)

[其他答案已评论了这是否是合理的设计。为了解决这个问题,我将这些问题放在一边。


0
投票

这可能是帮助

,以创建自定义先决条件例外,以避免如果条件多则]。在检查null
© www.soinside.com 2019 - 2024. All rights reserved.