java抛异常final_java – 捕获异常时是否有理由不使用final关键字?

The Java Language Specification 11.2.2在最终和非最终例外之间有所区别:

A throw statement (§14.18) whose thrown expression has static type E and is not a final or effectively final exception parameter can throw E or any exception class that the thrown expression can throw.

[…]

A throw statement whose thrown expression is a final or effectively final exception parameter of a catch clause C can throw an exception class E iff:

E is an exception class that the try block of the try statement which declares C can throw; and

E is assignment compatible with any of C’s catchable exception classes; and

E is not assignment compatible with any of the catchable exception classes of the catch clauses declared to the left of C in the same try statement.

In a uni-catch clause, an exception parameter that is not declared final (implicitly or explicitly) is considered effectively final if it never occurs within its scope as the left-hand operand of an assignment operator.

换句话说,如果你不重新分配你的catch语句的e(如e = new SomeOtherException();),它被隐式声明为final。

所以我只能得出结论,它没有什么区别,除非在catch块中修改异常,唯一可以想出的例子是:

public void method1() throws IOException {

try {

throw new IOException();

} catch (Exception e) { // e is not modified in catch => implicitly final

throw e; //compiles OK

}

}

//it works because method1 is semantically equivalent to method2:

public void method2() throws IOException {

try {

throw new IOException();

} catch (final Exception e) {

throw e;

}

}

public void method3() throws IOException {

try {

throw new IOException("1");

} catch (Exception e) {

e = new IOException("2"); //e modified: not implicitly final any more

throw e; //does not compile

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java语言中的关键字是指具有特殊含义的单词,这些单词在Java程序中具有特定的用途,不能作为标识符或变量名使用Java中共有50个关键字,其中包括48个保留关键字和2个特殊关键字。 以下是Java中的各种关键字: 1. abstract:用于定义抽象类和抽象方法。 2. assert:用于调试程序进行断言判断,如果条件不成立将出AssertionError异常。 3. boolean:用于定义布尔类型变量,只能取值true或false。 4. break:用于跳出循环语句。 5. byte:用于定义字节类型变量,取值范围为-128到127。 6. case:用于在switch语句中匹配选项。 7. catch:用于捕获异常。 8. char:用于定义字符类型变量。 9. class:用于定义类。 10. const:Java虽然保留了此关键字,但并没有使用,因此不能用于定义常量。 11. continue:用于跳过循环中的某个迭代。 12. default:用于switch语句中的默认选项。 13. do:用于定义do-while循环。 14. double:用于定义双精度浮点类型变量。 15. else:用于if语句中条件不成立执行的代码块。 16. enum:用于定义枚举类型。 17. extends:用于继承一个类或实现一个接口。 18. final:用于定义常量或不可变的变量,或者修饰类、方法、变量等,表示其不可再被继承、重写或修改。 19. finally:用于定义无论是否异常发生都需要执行的代码块。 20. float:用于定义单精度浮点类型变量。 21. for:用于定义for循环。 22. goto:Java虽然保留了此关键字,但并没有使用,因此不能跳转到标签。 23. if:用于定义条件语句。 24. implements:用于实现一个接口。 25. import:用于导入其他类的定义。 26. instanceof:用于判断一个对象是否属于某个类或实现了某个接口。 27. int:用于定义整型变量。 28. interface:用于定义接口。 29. long:用于定义长整型变量。 30. native:用于调用本地方法。 31. new:用于创建一个对象。 32. package:用于定义包。 33. private:用于定义私有成员,只能在当前类中访问。 34. protected:用于定义受保护的成员,只能在当前类及其子类和同一个包中访问。 35. public:用于定义公共成员,可以被任何类访问。 36. return:用于从方法中返回值。 37. short:用于定义短整型变量。 38. static:用于定义静态成员,只有一个拷贝,可以通过类名直接访问。 39. strictfp:用于声明浮点数计算具有严格的规范化行为。 40. super:用于引用父类的成员。 41. switch:用于定义switch语句。 42. synchronized:用于定义同步方法或同步代码块。 43. this:用于引用当前对象。 44. throw:用于异常。 45. throws:用于声明方法可能出的异常。 46. transient:用于声明不需要持久化的变量。 47. try:用于定义异常处理代码块。 48. void:用于定义无返回值的方法。 49. volatile:用于声明变量是易变的,即每次访问都需要从主存中读取。 50. while:用于定义while循环。 以上就是Java中的各种关键字,这些关键字Java程序中起着非常重要的作用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值