几个“ChildException”捕获块一个“异常”捕获块

时间:2012-05-22 12:39:20

标签: java exception-handling try-catch readability catch-block

几个ChildException catch块和一个Exception catch块之间有什么好处?通过更好,我的意思是以良好实践的方式

举例说明:

public static void main(String[] args) {
    System.out.println(Main.isNonsense1(null)); // false <- bad
    System.out.println(Main.isNonsense2(null)); // NullPointerException <- good
}

// More readable, less precise
public static boolean isNonsense1(String className) {
    try {
        Class.forName(className);
        String.class.getConstructor(String.class);
        className.getBytes("UTF-8");
        MessageDigest.getInstance("SHA-1").wait();
        return true;
    } catch (Exception e) {
        return false;
    }
}

// Less readable, more precise
public static boolean isNonsense2(String className) {
    try {
        Class.forName(className);
        String.class.getConstructor(String.class);
        className.getBytes("UTF-8");
        MessageDigest.getInstance("SHA-1").wait();
        return true;
    } catch (ClassNotFoundException e) {
        return false;
    } catch (NoSuchMethodException e) {
        return false;
    } catch (SecurityException e) {
        return false;
    } catch (UnsupportedEncodingException e) {
        return false;
    } catch (NoSuchAlgorithmException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
}

3 个答案:

答案 0 :(得分:4)

这与此问题有关:Catch multiple exceptions at once?

答案很好。关键是如果你抓住Exception,那么你应该处理你所知道的每一个案例,并throw所有其他案例。也就是说,只需在示例中捕获异常并返回false,就是个好主意。你可能会无意中发现一些你不想要的例外。

使用您的示例,这是我建议的代码:

public static boolean isNonsense2(String className) {
    try {
        Class.forName(className);
        String.class.getConstructor(String.class);
        className.getBytes("UTF-8");
        MessageDigest.getInstance("SHA-1").wait();
        return true;
    } catch (Exception e) {
        if (e instanceof ClassNotFoundException
                || e instanceof NoSuchMethodException
                || e instanceof SecurityException
                || e instanceof UnsupportedEncodingException
                || e instanceof NoSuchAlgorithmException
                || e instanceof InterruptedException) {
            return false;
        } else {
            throw e;
        }
    }
}

答案 1 :(得分:1)

我认为没有完全明确的答案。在你的情况下,我会像这样编码:

public static boolean isNonsense1(String className) {
    if(slassname==null) throw new IllegalArgumentException("className must not be null");
    try {
        Class.forName(className);
        String.class.getConstructor(String.class);
        className.getBytes("UTF-8");
        MessageDigest.getInstance("SHA-1").wait();
        return true;
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("provided class " + className + " not found");
    } catch (Exception e) {
        return false;
    }
}

对于我的风味,抛出NullPointerException总是很糟糕,这就是为什么我抛出IllegalArgumentException

答案 2 :(得分:0)

如果您对处理异常(根据最佳实践应该不感兴趣)不感兴趣,请不要理会明确的捕获。能够处理特定异常的重点是使您能够正确处理它们。