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)
Java Exception Keywords The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone. The try block must be followed by either catch or finally. The "catch" block is used to handle the exception.
There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.
There are three types of exception—the checked exception, the error and the runtime exception.
The exception handling fundamentals in Java revolve around the five keywords- try, catch, finally, throw, and throws. These keywords form the base of exception handling. All the exception handling mechanisms in Java are a result of these five keywords.
I always do mine the second way, because it allows for me to set cursor as final
. There's no reason I can see to have the assignment in the try clause if you are not actually trying to catch exceptions from it.
EDIT: Just to note, from the further discussion that has gone on. This is how I would handle the openCursor
call throwing an exception:
try
{
// ALLOCATE RESOURCE
final Cursor cursor = db.openCursor(null, null);
try
{
// USE RESOURCE
}
finally
{
// DISPOSE RESOURCE
cursor.close();
}
}
catch(OpenCursorException e)
{
// Handle this appropriately.
}
Note the clean separation of allocation, usage, and disposal. The only time this gets a little interesting is if the usage try
block throws the same exception that you're catching for the allocation try
block. (IOException
would be a particularly good example of this, as opening and reading can both throw one.) In that case, everything will still clean and dispose correctly, but you might incorrectly attribute failure to an initialization exception instead of a usage exception. In this case, you will want to catch the exception(s) in the inner try
block and handle them immediately in there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With