gpt4 book ai didi

java - JDK 7 捕获多种异常类型并通过改进的类型检查重新抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:06 26 4
gpt4 key购买 nike

在 Java 7 之前,如果我们必须从方法中重新抛出异常,那么我们将不得不采用以下两种方式之一,

public void rethrowException(String exceptionName) throws FirstException, SecondException{
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (FirstExceptione) {
throw e;
}catch (SecondException) {
throw e;
}
}

第二个是,

public void rethrowException(String exceptionName) throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}

根据我的理解,新 Java 7.0 已经改进了一种方式,您可以捕获异常的广泛异常,并且仍然在方法定义中保留窄异常,就像下面的代码一样,

public void rethrowException(String exceptionName)
throws FirstException, SecondException {
try {
// ...
}
catch (Exception e) {
throw e;
}
}

Java SE 7 编译器可以判断语句throw e抛出的异常一定有来自 try block ,try block 抛出的唯一异常可以是 FirstException 和第二异常。即使 catch 子句的异常参数 e 是 Exception 类型,编译器可以确定它是 FirstException 或 SecondException 的实例。这如果 catch 参数被分配给 catch block 中的另一个值,则分析将被禁用。然而,如果 catch 参数被赋值给另一个值,则必须在 Exception 中指定异常类型方法声明的 throws 子句。

来自 Oracle 文档,

详细来说,在 Java SE 7 及更高版本中,当您在 catch 子句中声明一种或多种异常类型时,并重新抛出此 catch block 处理的异常,编译器验证重新抛出的异常满足以下条件:

1) The try block is able to throw it.
2) There are no other preceding catch blocks that can handle it.
3) It is a subtype or supertype of one of the catch clause's exception parameters.
4) In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of
the catch clause's exception parameters. A compiler from a release prior to Java SE 7 generates
the error, "unreported exception Exception; must be caught or declared to be thrown" at the
statement throw e. The compiler checks if the type of the exception thrown is assignable to any
of the types declared in the throws clause of the rethrowException method declaration. However,
the type of the catch parameter e is Exception, which is a supertype, not a subtype, of
FirstException andSecondException.

我没能从理论上理解第 3 点和第 4 点。有人可以在上下文中解释我上面提到的代码吗?

最佳答案

考虑以下情况:

您的应用程序抛出以下异常之一 FirstExceptionSecondExceptionException

ExceptionFirstExceptionSecondException 的父类(super class)型,因为它们扩展了 Exception

SecondException extends FirstException 也应该适用。所以 FirstExceptionSecondExeption 的父类(super class)型,SecondExceptionFirstException 的子类型。

现在我们有了一个总是抛出 SecondException 的方法。

第一种情况:

try {
[...]
} catch(SecondException se) {
// Exception gets always caught in here
[...]
} catch(FirstException fe) {
[...]
} catch(Exception e) {
[...]
}

第二种情况:

try {
[...]
} catch(Exception e) {
// Exception gets always caught in here
// because Exception is supertype of all other Exception
[...]
} catch(FirstException fe) {
[...]
} catch(SecondException se) {
// is never called.
[...]
}

你明白这一点了吗?

关于java - JDK 7 捕获多种异常类型并通过改进的类型检查重新抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com