Java throws 不写,从JDK 1.7开始,从catch块抛出异常对象不需要throws子句!为什么会这样呢?...

I came across a weird scenario in java today while coding around. I have a try..catch block in my method which does not have any throws clause and I am able to throw the exception object caught in the catch block. It is an object of the Exception class, hence it is not an unchecked exception. Also, It is not printing the stacktrace if exception arises instead the exception is just getting swallowed.

Below is my code example,

public class ExceptionTest {

public void test() {

try

{

// Some code which may throw exception.

}

catch(Exception ex)

{

// Compiler should ask me to have a *throws Exception* in the signature, when I am throwing an exception object.

throw ex;

}

}

}

However, if I am throwing a new exception object instead of the caught exception object, compiler is asking me to have a throws clause in the method signature.

N.B: I am facing this situation when running in Java 7 or 8.

I am wondering, where is the thrown object going to? Anyone with any idea on this please...

解决方案

You'll see this if the code in the try block can't throw any checked exception. At that point, the compiler knows that the only kind of exception caught by the catch block has to be an unchecked exception, and so it can therefore be rethrown. Note that if you assigned a different value to ex within the catch block, the compiler would no longer be able to have that assurance. At the moment, ex is effectively final.

If you try to call something that is declared to throw a checked exception within the try block, the code will fail to compile as expected.

For example:

public class ExceptionTest {

public void test() {

try {

foo();

} catch(Exception ex) {

throw ex;

}

}

public void foo() throws java.io.IOException {

}

}

Gives an error of:

ExceptionTest.java:12: error: unreported exception IOException; must be caught or declared to be thrown

throw ex;

^

As for where the exception "goes" - if the code in the try block throws an unchecked exception, it gets propagated as normal. Try this:

public class ExceptionTest {

public static void main(String[] args) {

test();

}

public static void test() {

try {

String x = null;

x.length();

} catch(Exception ex) {

throw ex;

}

}

}

Running that gives the following output, as expected:

Exception in thread "main" java.lang.NullPointerException

at ExceptionTest.test(ExceptionTest.java:10)

at ExceptionTest.main(ExceptionTest.java:4)

JLS 11.2.2 documents what exceptions a statement can throw - your code will only compile if there are no checked exceptions that can be thrown.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值