Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 494729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:31:29+00:00 2026-05-13T05:31:29+00:00

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

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:31:29+00:00Added an answer on May 13, 2026 at 5:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll preface this question by stating I have historically used tables for my HTML
I have this table: create table demo ( key number(10) not null, type varchar2(3)
I have the following code which works fine however historically and again now I
Historically I've mainly written web apps in Django, but now I'm increasingly finding that
I'd like to have historical memory of the test cases I usually do to
Historically, I've always used mysql_real_escape_string() for all input derived from users that ends up
I have historically used a monolithic approach to PHP coding. That is, I write
Due to incomplete understanding of eclipse infrastructure I have one problem. Historically we have
Structured programming languages typically have a few control structures, like while , if ,
We have a website that we would like to extrapolate user controls from and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.