「ブラインド」再スローを使用して、チェック済みの例外を渡します。これを使用して、チェック済み例外をスローするラムダを使用できない Streams API を通過させました。たとえば、ThrowingXxxxx 機能インターフェイスがあるため、チェック済みの例外を渡すことができます。
これにより、呼び出し先がチェック例外を許可しないインターフェイスを介して渡す必要があることを知る必要なく、呼び出し元でチェック例外を自然にキャッチできます。
try {
// some code that can throw both checked and runtime exception
} catch (Exception e) {
throw rethrow(e);
}
呼び出し元のメソッドで、チェック済み例外を再度宣言できます。
public void loadFile(String file) throws IOException {
// call method with rethrow
}
/**
* Cast a CheckedException as an unchecked one.
*
* @param throwable to cast
* @param <T> the type of the Throwable
* @return this method will never return a Throwable instance, it will just throw it.
* @throws T the throwable as an unchecked throwable
*/
@SuppressWarnings("unchecked")
public static <T extends Throwable> RuntimeException rethrow(Throwable throwable) throws T {
throw (T) throwable; // rely on vacuous cast
}
例外を処理するためのさまざまなオプションが多数あります。それらのいくつかを使用します。
https://vanilla-java.github.io/2016/06/21/Reviewing-Exception-Handling.html