有没有办法压制" catch分支相同"警告?在这种情况下无法使用多次捕获

时间:2016-09-26 15:18:12

标签: java android suppress-warnings

使用API​​ 17处理Android应用程序,目前需要做一些反思才能完成任务。 ReflectiveOperationException仅适用于API 19及更高版本,但这很好,因为我可以单独捕获每个异常。

问题在于,当我这样做时,我得到一个警告,说捕获分支是相同的,可以使用多捕获(或使用我想要避免的异常)来编写。但是当我将捕获量写成多次捕获时,我得到的错误是因为不是API 19而无法使用ReflectiveOperationException类。

简单地说,我只想抑制警告,但除了只做@SuppressWarning("所有")

之外,我找不到任何匹配的东西。

对于上下文,这里是警告/错误:

// Error: "Multi-catch with these reflection exceptions requires API level 19 (current min is 15) 
// because they get compiled to the common but new super type ReflectiveOperationException.
// As a workaround either create individual catch statements, or catch Exception."
try {
    return someMethodThatThrowsExceptions();
} catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(e); 
}

// Warning: 'catch' branch identical to 'InstantiationException | IllegalAccessException' branch"
try {
    return someMethodThatThrowsExceptions();
} catch (InstantiationException e) {
    throw new RuntimeException(e); 
} catch (IllegalAccessException e) {
    throw new RuntimeException(e); 
} catch (NoSuchMethodException e) {
    throw new RuntimeException(e); 
} catch (InvocationTargetException e) {
    throw new RuntimeException(e); 
} 

编辑:添加了我正在处理的所有捕获,最初只有两个

2 个答案:

答案 0 :(得分:9)

/api应该可以解决问题。

答案 1 :(得分:1)

为了抑制某些警告我有这样的效用函数:

public static <T> T get( T value )
{
    return value;
}

因此,您可以在投放之前将e提供给此get()函数,从而使两个catch子句不相等。