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/
相关推荐
The entire Java language, including Java 7-specific features such as switch on string, try-with-resources, final rethrow, multicatch, and SafeVarargs A huge assortment of Java 7 APIs, beginning with ...
java8 看不到源码SLF4J 的 FindBugs 错误模式 本产品可帮助您验证 SLF4J 1.6、1.7 和 1.8 的使用情况。 适用于 Java8 及更高版本。 要将此插件与 SonarQube 一起使用,请参阅 。 要在编译时检测问题,请参阅 。 动机...
// A better C Program! #include #define N 200 void strUpper(char *s); void strLower(char *s); int main(){ char ms[N]; printf("Input ms: "); gets(ms); strUpper(ms); puts(ms); strLower(ms); ...
var rethrow = require ( 'rethrow' ) ; 有关用法示例,请参见。 原料药 在上下文中将给定的err重新抛出给给定lineno filename中有问题的模板表达式。 参量 err {Error} :错误对象 filename {String} :模板的...
错误处理基于异常的错误处理依赖于std :: current_exception和std :: rethrow_exception。 如果您预计会遇到许多错误情况,这些速度可能会很慢。其他实施std :: future 理想情况下,我们可以将核心库功能用于期货...
测试者一掷千金 描述 不同测试人员: 将异常抛出另一个异常。 将异常抛出到catch中-相同的异常。 抛出异常到catch中-两种不同的异常。 将日志错误扔到catch中。... 将日志错误扔到catch中。... :)
Testcase for proper handling of rethrow.
linux下使用nodejs 连接数据库mysql
var rethrow = require ( 'template-error' ) ; rethrow ( '\n<%= a %>\n<%= b %>' , { data : { a : 'aaa' } } ) ; 当未定义模板变量时(例如上例中的<%= b %> ,而不是得到一条神秘的错误...
支持选项rethrow: true | false; // rethrow catched errorverbose: true | false; //show verbose logreporter: String; //report function name, which should be globally accessed. default name is ...
% 索引错误% 抓住% % 可能在这里做一些家务% rethrow(lasterror); % 结尾% % >> 测试错误% ??? 试图访问 a(4711); 索引越界... % % >> 分散% ??? 试图访问 a(4711); 索引越界... % Error in ==> testdisperror at...