Use Java7 try-with-resources statement for Cursor close processing

Overview

I will show you how to use the ** try-with-resources ** statement added in Java7 to briefly write the process of closing the Cursor used when retrieving values from the database.

Code that uses the try-with-resources statement

try (Cursor c = contentResolver.query(uri, projection, selection, selectionArgs, null)) {
    ... // (Processing using cursor)
}

If you write the code to get the data from the Content Provider using the try-with-resources statement, you'll get the code above.

Because the try-with-resources statement automatically calls the Cursor # close () method when exiting the block inside the try. You no longer have to explicitly call close ().

What happens if you don't use the try-with-resources statement

If you write the code in the try-finally statement without using the try-with-resources statement, it will be as follows.

Cursor c = contentResolver.query(uri, projection, selection, selectionArgs, null);
try {
    ... // (Processing using cursor)
} finally {
    if (c != null) {
        c.close()
    }
}

The code becomes longer as the finally clause is written. Also, you have to check that cursor is not null before closing, so In addition to making the code longer, there is a risk of forgetting the null judgment and causing NPE.

Another example of not using the try-with-resources statement is code that doesn't even use the try statement:

Cursor c = contentResolver.query(uri, projection, selection, selectionArgs, null);
... // (Processing using cursor) ★
if (c != null) {
    c.close()
}

The problem with this writing is that the cursor will not be closed if an exception occurs at the star. Therefore, it is better to avoid it.

Summary

When using cursor, I think you should write it in a try-with-resources statement. You don't have to worry about the tedious process, and it seems to be a concise code.

Recommended Posts

Use Java7 try-with-resources statement for Cursor close processing
Java for statement
[Java] for statement / extended for statement
(Memo) Java for statement
[Processing × Java] How to use variables
java (use class type for field)
[Processing × Java] How to use arrays
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
Java, for statement / while statement starting from beginner
[Java] What should I use for writing files?
Simple obstacle racing made with processing for Java
Use Java external library for the time being
Let's use Java New FileIO! (Introduction, for beginners)
For JAVA learning (2018-03-16-01)
Java thread processing
2017 IDE for Java
Java string processing
[Java] Use Collectors.collectingAndThen
[Java] Multi-thread processing
[Java] Stream processing
Java switch statement
java iterative processing
[Java basics] Let's make a triangle with a for statement
How to use Truth (assertion library for Java / Android)
Use Watson Conversation as NLP (Java) (Natural Language Processing)
I tried using an extended for statement in Java
[Beginner] Java variable / logical operator / extended for statement [Note 22]
How to loop Java Map (for Each / extended for statement)