2017-06-29 76 views
0

我試圖寫一個自動的reactiv方式傳播檢查的異常,而無需編寫鍋爐板代碼與我的運營商內部的靜態塊的工具:rxjava反應器,工具傳播例外

public class ReactRethrow { 
     public static <T, R> Function<T, R> rethrow(Function<T, R> catchedFunc) { 

      return t -> { 
      try { 
       return catchedFunc.apply(t); 
      } catch (Exception e) { 
       throw Exceptions.propagate(e); 
      } 
     }; 
    } 
} 

但還是老樣子抱怨關於IOException在這裏:

Flux.fromArray(resources).map(ReactRethrow.rethrow(resource -> Paths.get(resource.getURI()))) 

有什麼想法嗎?

回答

1

好是有原因的,我不清楚地瞭解你必須採取的參數,拋出異常等申報特定functionalInterface功能:從這裏https://leoniedermeier.github.io/docs/java/java8/streams_with_checked_exceptions.html

public class ReactRethrow { 
    public static <T, R> Function<T, R> rethrow(FunctionWithCheckeException<T, R> catchedFunc) { 

     return t -> { 
      try { 
       return catchedFunc.call(t); 
      } catch (Exception e) { 
       throw Exceptions.propagate(e); 
      } 
     }; 
    } 

    @FunctionalInterface 
    public interface FunctionWithCheckeException<T, R> { 
     R call(T t) throws Exception; 
    } 

}