JAVA为什么需要重新抛出异_关于java:重新抛出异常:为什么方法在没有throws子句的情况下编译?...

在下面的源代码中,我重新抛出一个Exception。

为什么没有必要将throws关键字放在方法的签名上?

public void throwsOrNotThrowsThatsTheQuestion() {

try {

// Any processing

} catch (Exception e) {

throw e;

}

}

你确定你可以做一个检查异常?

这个源可能无法编译

Eclipse中似乎存在一个错误(Juno Service Release 1 Build id:20121004-1855),因为它没有在您的示例中显示任何错误。 IntelliJ显示预期的行为(未处理的异常错误)。 也许这就是你所看到的

关于已检查与未检查的异常的好帖子:stackoverflow.com/questions/6115896/

你甚至可以抓住并重新抛出Throwable。 有趣的是,java7仍未被广泛采用,其功能尚不为人所知。 但是人们很可能会快速地使用java8。

此行为似乎仅在Java 1.7上发生。使用1.6进行编译时,我收到以下编译器错误消息:

c:\dev\src\misc>javac -source 1.6 Main.java

warning: [options] bootstrap class path not set in conjunction with -source 1.6

Main.java:22: error: unreported exception Exception; must be caught or declared

to be thrown

throw e;

^

1 error

1 warning

但是使用Java 1.7,它可以编译。

c:\dev\src\misc>javac -source 1.7 Main.java

c:\dev\src\misc>

...直到我在try块中实际抛出Exception:

public static void throwsOrNotThrowsThatsTheQuestion() {

try {

// Any processing

throw new IOException("Fake!");

} catch (Exception e) {

throw e;

}

编译...

c:\dev\src\misc>javac -source 1.7 Main.java

Main.java:22: error: unreported exception IOException; must be caught or declare

d to be thrown

throw e;

^

1 error

看起来Java 1.7足够聪明,可以通过分析try块代码来检测可能抛出的Exception(s)的类型,其中1.6只看到类型为Exception的throw e;并且只是给出了一个错误为了那个原因。

将其更改为抛出RuntimeException使其按预期编译,因为一如既往,未经检查的Exception不需要throws子句:

public static void throwsOrNotThrowsThatsTheQuestion() {

try {

// Any processing

throw new RuntimeException("Fake!");

} catch (Exception e) {

throw e;

}

编译...

c:\dev\src\misc>javac -source 1.7 Main.java

c:\dev\src\misc>

说明

这是发生了什么:

Java 7引入了更具包容性的类型检查。引述...

Consider the following example:

static class FirstException extends Exception { }

static class SecondException extends Exception { }

public void rethrowException(String exceptionName) throws Exception {

try {

if (exceptionName.equals("First")) {

throw new FirstException();

} else {

throw new SecondException();

}

} catch (Exception e) {

throw e;

}

}

This examples's try block could throw either FirstException or SecondException. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.

However, in Java SE 7, you can specify the exception types FirstException and SecondException in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be FirstException and SecondException. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either FirstException or SecondException:

(强调我的)

public void rethrowException(String exceptionName)

throws FirstException, SecondException {

try {

// ...

}

catch (Exception e) {

throw e;

}

}

java.lang.Exception是一个经过检查的异常,因此无法工作甚至编译。它可以使用unckeched(java.lang.RuntimeException)。

无论是否在catch块中抛出异常,绝对没有区别。

编译器错误看起来像这样(取决于编译器):

java: unreported exception java.lang.Exception; must be caught or declared to be thrown

编辑:如果你从未真正抛出异常,Java 7可以处理这种情况

我尝试使用java 7并实际编译。

Why is not necessary to put the throws keyword on the method's

signature?

你可以把因为你的// Any processing没有抛出任何检查异常。

例:

这编译好了。

public void throwsOrNotThrowsThatsTheQuestion() {

try {

throw new RuntimeException();

} catch (Exception e) {

throw e;

}

这不会编译,需要添加throws子句。

public void throwsOrNotThrowsThatsTheQuestion() {

try {

throw new Exception();

} catch (Exception e) {

//do something like log and rethrow

throw e;

}

}

这是从java 7开始工作的。在以前的版本中引发了异常。更多信息在java 7中重新抛出

我打赌你这个答案错了??。你有编译吗?

@hgoebl我做了,nachokk是正确的。

是的,使用Java 7,我尝试使用netbeans,当我3个月前发现这个时,我感到很惊讶,我不知道在以前的版本中是否有这项工作,我认为和@hgoebl一样

从远处看堆栈的最佳答案

好吧我信你。 Java越来越聪明了!

@nachokk同样的代码抛出一个异常JAVA 6 :-)。,看起来JAVA 7中有些变化。非常令人惊讶。 mindview.net/Etc/Discussions/CheckedExceptions

@SathishJayapal是的,这个功能是自java7以来

如果您检查了已检查的异常,则需要将其放在throws列表中

public void retrhowChecked() throws Exception {

try {

throw new IOException();

} catch(Exception e) {

throw e;

}

}

如果你抛出一个未经检查的异常,你不需要将它放在throws列表中,你可以使用它来在未经检查的异常中包含一个已检查的Exception,以避免破坏使用此方法的代码,如果你在这样的方法中更改了有问题的方法改变后的方式可能会产生一个检查过的异常。但你必须小心,检查Exception是否有待处理!

public void retrhowUnchecked() {

try {

throw new IOException();

} catch(Exception e) {

throw new RuntimeException(e);

}

}

在此处阅读有关例外的更多信息。

throw new Exception(); is something you should never do in a catch

block, but you may have to or want to do throw new

SomeException(throwable); (preserving the full stack trace) instead

of throw throwable; in order to conform to the API of your method,

e.g. when it declares to throw SomeException but you're calling code

that might throw an IOException that you don't want to add to you

method's throws clause.

The probably most common case is new RuntimeException(throwable); to

avoid having a throws clause altogether.

当您对方法使用throws时,这意味着将调用该方法的语句必须用try catch块包围。

但是如果该方法已经包含try catch块,则不需要thorws声明,因为该方法抛出的异常仅在那里处理。

调用此方法的语句不需要用try catch块包围。

希望这能清除你的怀疑。

例外不是"仅在那里处理",因为它重新抛出它。

我觉得你读错了我说的@ajb。请再次阅读并理解我写的每个单词。然后做出决定。

好的,我做到了。我的决定是你写的内容非常模糊 - 当你使用"方法"这个词时,我不知道你是指帖子中的方法,该方法调用的方法,还是调用方法的方法。无论如何,我们已经确定throwsOrNotThrowsThatsTheQuestion可能需要或不需要throws声明,具体取决于"任何处理"代码中的内容。

用你的话来说,似乎你理解我的意思和帖子中包含的内容。我只是需要同样的东西。谢谢 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值