There is nothing wrong with this, assuming you need to nest the try-catch blocks like this in the first place. Maroun's answer would be better for the example you provided. An example where the approach you suggested would be more suitable might be if you have lots of local variables involved in the "Cursor" clean-up:
openDatabaseConnection();
try {
Cursor cursor = openCursor();
CursorHelper helper = new CursorHelper(cursor);
String cursor_id = cursor.getId();
String something_else = cursor.getSomethingElse();
try {
useCursor(cursor_id, something_else, helper);
} finally {
closeCursor(cursor_id, something_else, helper);
}
} catch (Exception e) {
genericLogError();
} finally {
closeDatabaseConnection();
}
The parent try block will catch the exception thrown by the nested one, but the nested finally
block will be called first. This keeps the scope of the cursor variables encapsulated within the first try/catch
block.
If you wanted to pull all this code into one try/catch
block you would have to declare some of your variables outside the block, which can start to affect readability:
openDatabaseConnection();
CursorHelper helper = null;
String cursor_id = null;
String something_else = null;
try {
Cursor cursor = openCursor();
helper = new CursorHelper(cursor);
cursor_id = cursor.getId();
something_else = cursor.getSomethingElse();
useCursor(cursor_id, something_else, helper);
} catch (Exception e) {
genericLogError();
} finally {
if (cursor_id != null) {
closeCursor(cursor_id, something_else, helper);
}
closeDatabaseConnection();
}