15 Ekim 2019 Salı

Closer Sınıfı

Giriş
Şu satırı dahil ederiz
import com.google.comon.io.Closer;
close metodu
Örnek
Şöyle yaparız.
public void foo() throws IOException {
  Closer closer = Closer.create();
  try {
    InputStream in = closer.register(openInputStream());
    OutputStream out = closer.register(openOutputStream());
    // do stuff with in and out
  } catch (Throwable e) { // must catch Throwable
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}
Örnek
Şöyle yaparız.
Closer closer = Closer.create();
try {
  A a = closer.register(new A());
  B b = closer.register(new B());
  C c = closer.register(new C());
  // ...
} catch (Throwable e) { // must catch Throwable
  throw closer.rethrow(e);
} finally {
  closer.close();
}
rethrow metodu
Şöyle yaparız.
} catch (Throwable t) {
  throw closer.rethrow(e, MyOwnCheckedException.class);
} finally {
  closer.close();
}

Hiç yorum yok:

Yorum Gönder