unreported exception when throwing from a lambda in a Completable Future

前端 未结 1 1053
清歌不尽
清歌不尽 2020-12-12 04:25

When I compile the code below, I get the following error:

/home/prakashs/composite_indexes/src/main/java/com/spakai/composite/TwoKeyLookup.java:22: error: un         


        
相关标签:
1条回答
  • 2020-12-12 04:39

    Checked Exceptions are much older than Java promises and do not work well with them as of Java 8. Technically speaking, BiFunction does not declare throwing any checked Exception. As such, your findCommonMatch, which you pass to thenCombine, can not throw them either.

    Make NoMatchException unchecked by inheriting from RuntimeException. Also remove the misleading throws declaration from the lookup method — it is not throwing anything — the code, being encapsulated within promise, is going to throw, not method creating promise.

    Exceptions thrown within promises are by design completely invisible to code, that creates them and subscribes to them. Instead you are usually expected to use unchecked exceptions and handle them in a way, specific for particular promise library (see documentation of CompletionStage for details on it's exception handling facilities).

    0 讨论(0)
提交回复
热议问题