OOP-Exception try resrource

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:17 AM on 5/21/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

21 Terms

1
New cards

What is try-with-resources in Java?

A try statement that automatically closes resources after use

2
New cards

When was try-with-resources introduced?

Java 7

3
New cards

What is a resource in try-with-resources?

An object that must be closed after use (e.g., files, streams, DB connections)

4
New cards

What interface must a resource implement?

AutoCloseable

5
New cards

Example of AutoCloseable classes

FileInputStream, BufferedReader, Scanner, Connection

6
New cards

Basic syntax of try-with-resources

try(Resource res = …) { code }

7
New cards

What happens to resources after try block?

They are automatically closed

8
New cards

Do we need finally block with try-with-resources?

No, it's optional because closing is automatic

9
New cards

What happens if exception occurs?

Resource is still closed automatically

10
New cards

Can we use multiple resources?

Yes, separated by semicolons

11
New cards

Example with multiple resources

try(r1 = …; r2 = …) { }

12
New cards

Order of closing resources

Closed in reverse order of declaration

13
New cards

Can we catch exceptions in try-with-resources?

Yes, using catch blocks

14
New cards

Can we use finally with try-with-resources?

Yes, but usually not needed

15
New cards

What problem does try-with-resources solve?

Resource leaks (forgetting to close resources)

16
New cards

Difference between try-finally and try-with-resources

try-finally requires manual close; try-with-resources is automatic

17
New cards

What happens if close() throws exception?

It becomes a suppressed exception

18
New cards

What is a suppressed exception?

An exception thrown during resource closing while another exception already exists

19
New cards

How to get suppressed exceptions?

using getSuppressed() method

20
New cards

Can we declare resource outside try?

Yes (since Java 9), if it is effectively final

21
New cards

Example Java 9 syntax

Resource r = …; try(r) { }