The specification for the Cleaner class states that, "All exceptions thrown by the cleaning action are ignored."
This matches the behavior of finalizer, the now-deprecated mechanism for GC-triggered cleanup that Cleaner can be used in place of.
However, this situation leaves the developer uninformed about problems that occur during resource cleanup,including potential resource leaks.
With Cleaner, we can do better.
The current API allows a Cleaner to be created with a user-supplied ThreadFactory. This thread factory can set an UncaughtExceptionHandler on the thread(s) it creates. However the Cleaner spec mandates that exceptions thrown in the cleaning action are ignored. The spec could be updated to allow exceptions to be delivered to such an UncaughtExceptionHandler.
We might also consider adding a method directly on Cleaner to allow a user to set an UncaughtExceptionHandler to receive exception thrown by the Cleaning Actions run on the Cleaner.
This matches the behavior of finalizer, the now-deprecated mechanism for GC-triggered cleanup that Cleaner can be used in place of.
However, this situation leaves the developer uninformed about problems that occur during resource cleanup,including potential resource leaks.
With Cleaner, we can do better.
The current API allows a Cleaner to be created with a user-supplied ThreadFactory. This thread factory can set an UncaughtExceptionHandler on the thread(s) it creates. However the Cleaner spec mandates that exceptions thrown in the cleaning action are ignored. The spec could be updated to allow exceptions to be delivered to such an UncaughtExceptionHandler.
We might also consider adding a method directly on Cleaner to allow a user to set an UncaughtExceptionHandler to receive exception thrown by the Cleaning Actions run on the Cleaner.