Java异常处理 - 样式 [英] Java Exception Handling - Style

查看:100
本文介绍了Java异常处理 - 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Cursor cursor = null; 

历史上,我一直写下我的异常处理代码:
try {
cursor = db.openCursor(null,null);
// do stuff
} finally {
if(cursor!= null)cursor.close();
}

但最近,由于可读性和懒惰的原因,我已经开始做:

 光标cursor = db.openCursor(null,null); 
try {
// do stuff
} finally {
cursor.close();
}

我的错误是赋值给游标(jdbc句柄,无论如何)的try-catch-finally块?



除了JVM实际上在作业中,或任务和第一行之间的任何东西在try块我不知道我的旧风格是否借给任何额外的价值,第二个当然更可读和简洁。文学一般始终与第一种风格一起走



编辑 - 假设我很高兴在初始化游标不被这个代码块捕获时由openCursor抛出的任何异常,我唯一关心的例子是关闭光标,如果分配&打开。还有假设我正在测试nulls 等..等.. yadda ... yadda ...(我已经改变了例子来反映这一点,这不是我的问题的焦点,所以我没有包含在第一个版本)

解决方案

如果你在最后完成的是关闭光标,然后是第二个形式是正确的如果openCursor()失败,您将永远不会关闭游标。该变量的值甚至不会被设置。



正如其他人所指出的那样,注意事项是,如果您正在进行额外的初始化,需要自己清理,那么将在逻辑上必须进入最终{}并相应地更改范围。虽然我在这种情况下争论重组。



底线:写第一个版本是不必要的复杂。第二个版本是正确的。



编辑:将我的其他评论纳入后代...



第一个例子可能似乎是无害的,因为它所做的只是添加一堆不必要的代码。 (完全不需要,如果不清楚)然而,在经典的更多的代码意味着更多的潜在的错误的时尚,有一个隐藏的骗子。



原因//做某事代码无意中清除游标变量,那么您将默认地泄漏游标,而在您至少得到NullPointerException之前。由于额外的代码绝对没有任何用处,因此额外的风险是完全不必要的。



因此,我愿意称之为简单错误的第一个例子。我肯定会在代码审查中标记。


Historically I have always written my Exception handling code like this:

    Cursor cursor = null;
    try {
        cursor = db.openCursor(null, null);
        // do stuff
    } finally {
        if (cursor != null) cursor.close();
    }

But recently, for reasons of readability and laziness, I have started to do this:

        Cursor cursor = db.openCursor(null, null);
        try {           
            // do stuff
        } finally {
            cursor.close();
        }

Am I wrong to have the assignment to cursor (jdbc handle, whatever) out of the try-catch-finally block?

Barring the JVM actually blowing up on the assignment, or inbetween the assignment and the first line of whatever is in the try block I'm not sure if my old style was lending any extra value, and the second is certainly more readable and concise. The literature generally always does go with the first style though.

EDIT - assume I'm happy for any exceptions thrown by openCursor while initialising the cursor not to be caught in this block of code, my only concern for this example is closing the cursor if it is assigned & opened. Also assume I'm testing for nulls etc.. etc.. yadda... yadda... (I have changed the example to reflect this, it wasn't the focus of my question so I didn't include it in the first version)

解决方案

If all you are doing in your finally is closing the cursor then the second form is correct. You will never have a cursor to close if openCursor() fails. The value of that variable won't even have been set.

As others allude to, the caveats are if you are doing additional initialization that requires its own clean up then that will logically have to go in the finally{} and change the scope accordingly. Though I'd argue for a restructuring in that case.

The bottom line: as written the first version is needlessly complicated. The second version is correct.

Edit: Incorporating my other comments for posterity...

The first example might seem harmless as all it does is add a bunch of needless code. (Completely needless, if that wasn't clear.) However, in classic "more code means more potential bugs" fashion, there is a hidden gotcha.

If for some reason the "// do something" code inadvertently clears the cursor variable then you will silently leak cursors whereas before you'd at least have gotten a NullPointerException. Since the extra code does absolutely nothing useful, the extra risk is completely unnecessary.

As such, I'm willing to call the first example "just plain wrong". I'd certainly flag it in a code review.

这篇关于Java异常处理 - 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆