`

zz - Better exception handling in Java 7 : Multicatch and final rethrow

    博客分类:
  • java
阅读更多

Better exception handling in Java 7 : Multicatch and final rethrow

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) {
2      logger.error(ex);
3      throw ex;
4 } catch (SecondException ex) {
5      logger.error(ex);
6      throw ex;
7 }

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) {
2      logger.error(ex);
3     throw ex;
4 }

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 :

1 try {
2      // some code
3 } catch (final Throwable ex) {
4      // some more code
5     throw ex;
6 }

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/ 

分享到:
评论

相关推荐

    java程序实例 事件处理

    **示例四:RethrowException.java** ```java public class RethrowException { public static void f() throws Exception { System.out.println("the original exception in f()"); throw new Exception("thrown...

    Dart基础语法.pdf

    Final and const - **final**: 不可变变量,只能被赋值一次。 ```dart final test1 = "test"; // test1 = "test1"; // 错误,因为final变量只允许赋值一次 ``` - **const**: 编译时常量,同时也是隐式final。 ...

    JAVA 7新特性1

    JAVA 7 新特性 —— 异常处理升级 JAVA 7 中引入了多个新特性,其中之一是异常处理升级。在 Java 7 中,catch 代码块得到了升级,可以在单个 catch 块中处理多个异常。这种特性可以减少代码重复度,并生成更少的...

    Java程序设计:chapter10 异常处理.ppt

    Java程序设计中的异常处理是确保程序健壮性和稳定性的关键机制。异常是在程序运行时遇到的不正常情况或错误,例如除数为零、数组越界等,它们会导致程序中断执行。异常处理主要包括以下几个方面: 1. **异常处理...

    Java_Programming_Exception_Example_code.rar_advanced java_java p

    在"Java高级编程RethrowException代码.rar"中,我们可以看到如何处理和重新抛出异常。在Java中,`try-catch`块用于捕获和处理异常。`try`块包含可能抛出异常的代码,`catch`块用于捕获并处理这些异常。如果在`try`块...

    Beginning Java 7

    ### Java 7 入门详解 #### 一、Java 7 概览 Java 7 是 Java 编程语言的一个重要版本,它引入了一系列新特性与改进,旨在提高开发效率和程序性能。本书《Beginning Java 7》为读者提供了一个全面的学习路径,不仅...

    staf+stax hands on

    - `<throw>` 和 `<rethrow>`:用于在脚本中引发或重新抛出异常。 2. **函数定义与调用**: - `<function>`:定义可重复使用的函数。 - `<call>`:调用已定义的函数。 - `<return>`:从函数返回值。 - `...

    C++Exception 异常处理 源码

    9. **`std::current_exception`和`std::rethrow_exception`**: 这两个函数用于获取当前活跃的异常对象和重新抛出它。这对于在异常处理过程中进行调试或记录信息非常有用。 10. **异常与析构函数**: 当异常发生...

    freemarker\Freemarker教程_中文版

    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); ``` ### 2. 数据模型(DataModel) 数据模型是Freemarker中用来传递给模版的数据结构。它可以是任何类型的Java对象,包括但不限于字符...

    Java throw Exception实现异常转换

    Java throw Exception实现异常转换 Java throw Exception实现异常转换是Java语言中的一种异常处理机制,它允许开发者将 checked exception 转换为 unchecked exception,从而使得异常处理变得更加灵活和便捷。本文...

    java8看不到源码-findbugs-slf4j:一个SpotBugs/FindBugs插件来验证SLF4J的使用

    java8 看不到源码SLF4J 的 FindBugs 错误模式 本产品可帮助您验证 SLF4J 1.6、1.7 和 1.8 的使用情况。 适用于 Java8 及更高版本。 要将此插件与 SonarQube 一起使用,请参阅 。 要在编译时检测问题,请参阅 。 动机...

    Qt程序crash信息的捕捉与跟踪Demo

    在该函数中,我们可以使用`std::rethrow_exception()`来重新抛出异常,以便在调试器中查看堆栈信息。同时,我们也可以在这个函数中收集其他有用的信息,如进程ID、线程ID和当前时间戳,这些都可以帮助我们追踪问题。...

    C# 编程规范大全

    // Log the exception and possibly rethrow } ``` **推荐做法**:尽量捕获具体的异常类型,并处理这些特定类型的异常。 ##### 不要在封闭类型中定义保护成员 (CA1047) **规则描述**:在封闭类型(即没有继承的...

    JavaPDF生成文档的总结及相关代码,Java生成PDF

    JavaPDF生成文档是一种常见的技术,尤其在企业级应用中,用于生成报告、发票、合同等正式文件。在Java中,有多个库可以帮助我们创建PDF文档,例如iText、Apache PDFBox、Flying Saucer等。本篇文章将深入探讨如何...

    Java教学中存在的问题及建议.pdf

    * Java 7版本中新增了Modularization模块化、对动态语言的支持、更多新的I/O APIs、对XML本地语言支持、Safe rethrow、Null dereference expressions、更好的类型推断、多重捕获Multi-catch、Swing应用框架等新特性...

    node.js连接docker启动的mysql数据库失败原因。

    node.js连接数据库的坑端口问题的坑。error connecting: Error: ER_ACCESS_DENIED_ERROR: Access denied for user ‘root’@’DESKTOP-NLN1UTH’ (using password: YES)加密规则的坑(docker启动) ...

    rethrow:重新抛出与模板渲染,编译或其他处理相关的错误,以获取报告

    var rethrow = require ( 'rethrow' ) ; 有关用法示例,请参见。 原料药 在上下文中将给定的err重新抛出给给定lineno filename中有问题的模板表达式。 参量 err {Error} :错误对象 filename {String} :模板的...

Global site tag (gtag.js) - Google Analytics