11 ms·
In the JVM the equivalent to RAII is implemented with try-with-resource/Autocloseable instead.
by pdpi 6y ago
In the JVM the equivalent to RAII is implemented with try-with-resource/Autocloseable instead.
- cesarb 6y agoIn my experience, try-with-resources is inferior to RAII, since it's very easy to forget (that is, it's very easy to do "Foo x = bar();" instead of "try (Foo x = bar()) { ... }"), leading to resource leaks. More than once I have added a finalizer to an AutoCloseable class just to warn loudly if close() hasn't been called; unfortunately, there's no way that I know of to suppress the finalizer once close() is called, so the GC still has to do all the work to call the finalizer even when the class was used correctly.