Try-With-Resources in Java

🟢 Beginner Level | Java Core Syntax

📖 Definition

try-with-resources automatically closes declared AutoCloseable stream resources upon block completion.

âš¡ Code Example

try (Scanner scanner = new Scanner(new File("data.txt"))) {
    // Scanner automatically closed
} catch (FileNotFoundException e) { e.printStackTrace(); }

🧭 Navigation

← 03. Try-Catch-Finally Blocks | Module Hub | 05. Custom Exceptions →