Explain try-with-resources statement
π‘ Concept: Try-With-Resources
Try-with-resources is a Java statement that ensures automatic closing of resources like streams and readers, preventing memory leaks and boilerplate code.
π Quick Intro
Introduced in Java 7, try-with-resources eliminates the need for finally blocks by auto-closing resources that implement the AutoCloseable
or Closeable
interface.
π§ Analogy
Think of try-with-resources like a smart dishwasher. You load it once (open the resource), and after itβs done (block completes), it automatically cleans and shuts off (closes the resource). No manual cleanup needed!
π§ Technical Explanation
- Resources declared in the try block must implement
AutoCloseable
. - Once the try block finishes, each resource is automatically closed in reverse order of creation.
- If an exception is thrown, all opened resources are still closed properly.
- It simplifies code and prevents common memory/resource leaks.
- Multiple resources can be declared separated by semicolons.
π― Use Cases
- β
Reading/writing files using
BufferedReader
orFileInputStream
- β Handling JDBC database connections
- β
Network streams or any
AutoCloseable
resources
π» Example: Try-With-Resources
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

β Interview Q&A
Q1: What is try-with-resources?
A: A try block that automatically closes resources implementing AutoCloseable.
Q2: When was try-with-resources introduced?
A: Java 7.
Q3: What interface must resources implement?
A: AutoCloseable or Closeable.
Q4: Can multiple resources be declared?
A: Yes, separated by semicolons.
Q5: Does try-with-resources prevent memory leaks?
A: Yes, by ensuring proper closure.
Q6: What happens on exception in the try block?
A: All declared resources are still closed.
Q7: What is the order of closing resources?
A: Reverse order of declaration.
Q8: Can we use catch or finally with try-with-resources?
A: Yes, they are optional.
Q9: Is try-with-resources mandatory for AutoCloseable?
A: No, but it's recommended.
Q10: Is it better than try-finally?
A: Yes, itβs more concise and safer.
π MCQs
Q1. What is the purpose of try-with-resources?
- Manually manage memory
- Auto-close resources
- Create exceptions
- Throw errors
Q2. Which Java version introduced try-with-resources?
- Java 6
- Java 7
- Java 8
- Java 5
Q3. Which interface must be implemented?
- Serializable
- Cloneable
- Runnable
- AutoCloseable
Q4. Can try-with-resources handle multiple resources?
- No
- Yes
- Only in Java 11
- Only with JDBC
Q5. Does try-with-resources replace finally block?
- No
- Yes, for resource closure
- Only in loops
- Not applicable
Q6. Are catch/finally blocks mandatory?
- Yes
- No
- Only catch
- Only finally
Q7. Order of resource closing?
- Same as declared
- Alphabetical
- Reverse of declaration
- Random
Q8. Is AutoCloseable the same as Closeable?
- Yes
- No
- They are unrelated
- Closeable extends AutoCloseable
Q9. Does try-with-resources reduce boilerplate?
- No
- Yes
- Sometimes
- Depends
Q10. What happens if exception occurs?
- Resources leak
- Only some closed
- Resources are still closed
- Program crashes
π‘ Bonus Insight
You can use try-with-resources with custom classes as long as they implement AutoCloseable
. Itβs not just for file and stream I/O!
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!