I’m happy to announce that an other improvement from the Project Coin has be marked for inclusion in Java 7 : Improved Exception Handling for Java, from Neal Gafter. This has been announced by Joe Darcy on his blog.
This improvement add two litlte improvements to exception handling :
- Multicatch : You’ll now be able to catch multi exceptions type in one catch block
- Final Rethow : Allows you to catch an exception type and it’s subtype and rethrow it without having to add a throws clause to the method signature.
Often, we have that kind of code :
1 |
} catch (FirstException ex) {
|
4 |
} catch (SecondException ex) {
|
But that code is heavy for nothing really interesting. A solution is to find a common supertype of these two exceptions type and catch just that type and rethrow it. But that can catch more exceptions than you want.
So now, with that new feature, you can do :
1 |
} catch (FirstException | SecondException ex) {
|
A lot more cleaner, isn’t it ?
And the second improvement is a little more complicated. Imagine that you want to catch all exceptions, make several operations and then rethrow it. The code isn’t hard to make, but the big problem is that you must add a throws clause to your method signature to manage the new exception launched by your code and this is not the objective. Now, you can do that without adding an exception throws clause :
3 |
} catch ( final Throwable ex) {
|
Using the final keyword it allows you to throw an exception of the exact dynamic type that will be throwed. So if an IOException occurs, an IOException will be throwed. Of course, you have to declare the exceptions not caught. You throws clauses will exactly the same if you use the code (in //some code) without catching anything but now you can do something if that happens.
I think multi-catch is a great feature, but for me the final rethrow is not often useful for programmers and perhaps a little weird using the final keyword.
zz address: http://www.baptiste-wicht.com/2010/05/better-exception-handling-in-java-7-multicatch-and-final-rethrow/
相关推荐
**示例四:RethrowException.java** ```java public class RethrowException { public static void f() throws Exception { System.out.println("the original exception in f()"); throw new Exception("thrown...
Final and const - **final**: 不可变变量,只能被赋值一次。 ```dart final test1 = "test"; // test1 = "test1"; // 错误,因为final变量只允许赋值一次 ``` - **const**: 编译时常量,同时也是隐式final。 ...
JAVA 7 新特性 —— 异常处理升级 JAVA 7 中引入了多个新特性,其中之一是异常处理升级。在 Java 7 中,catch 代码块得到了升级,可以在单个 catch 块中处理多个异常。这种特性可以减少代码重复度,并生成更少的...
Java程序设计中的异常处理是确保程序健壮性和稳定性的关键机制。异常是在程序运行时遇到的不正常情况或错误,例如除数为零、数组越界等,它们会导致程序中断执行。异常处理主要包括以下几个方面: 1. **异常处理...
在"Java高级编程RethrowException代码.rar"中,我们可以看到如何处理和重新抛出异常。在Java中,`try-catch`块用于捕获和处理异常。`try`块包含可能抛出异常的代码,`catch`块用于捕获并处理这些异常。如果在`try`块...
### Java 7 入门详解 #### 一、Java 7 概览 Java 7 是 Java 编程语言的一个重要版本,它引入了一系列新特性与改进,旨在提高开发效率和程序性能。本书《Beginning Java 7》为读者提供了一个全面的学习路径,不仅...
- `<throw>` 和 `<rethrow>`:用于在脚本中引发或重新抛出异常。 2. **函数定义与调用**: - `<function>`:定义可重复使用的函数。 - `<call>`:调用已定义的函数。 - `<return>`:从函数返回值。 - `...
9. **`std::current_exception`和`std::rethrow_exception`**: 这两个函数用于获取当前活跃的异常对象和重新抛出它。这对于在异常处理过程中进行调试或记录信息非常有用。 10. **异常与析构函数**: 当异常发生...
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); ``` ### 2. 数据模型(DataModel) 数据模型是Freemarker中用来传递给模版的数据结构。它可以是任何类型的Java对象,包括但不限于字符...
Java throw Exception实现异常转换 Java throw Exception实现异常转换是Java语言中的一种异常处理机制,它允许开发者将 checked exception 转换为 unchecked exception,从而使得异常处理变得更加灵活和便捷。本文...
java8 看不到源码SLF4J 的 FindBugs 错误模式 本产品可帮助您验证 SLF4J 1.6、1.7 和 1.8 的使用情况。 适用于 Java8 及更高版本。 要将此插件与 SonarQube 一起使用,请参阅 。 要在编译时检测问题,请参阅 。 动机...
在该函数中,我们可以使用`std::rethrow_exception()`来重新抛出异常,以便在调试器中查看堆栈信息。同时,我们也可以在这个函数中收集其他有用的信息,如进程ID、线程ID和当前时间戳,这些都可以帮助我们追踪问题。...
// Log the exception and possibly rethrow } ``` **推荐做法**:尽量捕获具体的异常类型,并处理这些特定类型的异常。 ##### 不要在封闭类型中定义保护成员 (CA1047) **规则描述**:在封闭类型(即没有继承的...
JavaPDF生成文档是一种常见的技术,尤其在企业级应用中,用于生成报告、发票、合同等正式文件。在Java中,有多个库可以帮助我们创建PDF文档,例如iText、Apache PDFBox、Flying Saucer等。本篇文章将深入探讨如何...
* Java 7版本中新增了Modularization模块化、对动态语言的支持、更多新的I/O APIs、对XML本地语言支持、Safe rethrow、Null dereference expressions、更好的类型推断、多重捕获Multi-catch、Swing应用框架等新特性...
node.js连接数据库的坑端口问题的坑。error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user ‘root’@’DESKTOP-NLN1UTH’ (using password: YES)加密规则的坑(docker启动) ...
var rethrow = require ( 'rethrow' ) ; 有关用法示例,请参见。 原料药 在上下文中将给定的err重新抛出给给定lineno filename中有问题的模板表达式。 参量 err {Error} :错误对象 filename {String} :模板的...
#### 异常的重新抛出(`rethrow`) 异常的重新抛出是指在`catch`块中再次抛出异常的行为。这通常发生在两种情况下:一种是在当前`catch`块无法处理异常时,另一种是在处理过程中触发了新的异常。异常的重新抛出只能...