Module qtjambi
Package io.qt.core

Class QScopeGuard

java.lang.Object
io.qt.core.QScopeGuard
All Implemented Interfaces:
AutoCloseable

public class QScopeGuard
extends Object
implements AutoCloseable

QScopeGuard is a class of which the sole purpose is to run the function f at the end of a try-with-resource block. This is useful for guaranteeing your cleanup code is executed, whether the function is exited normally, exited early by a return statement, or exited by an exception.

See QScopeGuard.

Example:

QIODevice device = ...
try(var guard = new QScopeGuard(device::close)){
    if(!device.open())
        guard.dismiss();
    else
        device.write(...);
}
  • Constructor Details

    • QScopeGuard

      public QScopeGuard​(Runnable function)
      Create a scope guard that will execute function at the end of the scope.
      Parameters:
      function - runnable to be executed
  • Method Details

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • dismiss

      public void dismiss()
      Disarms the scope guard, so that the function F will not be called at the end of the scope.