Java 8 Lambda function that throws exception?

后端 未结 26 1706
臣服心动
臣服心动 2020-11-22 03:14

I know how to create a reference to a method that has a String parameter and returns an int, it\'s:

Function         


        
26条回答
  •  清酒与你
    2020-11-22 03:22

    If you don't mind using a third party library, with cyclops-react, a library I contribute to, you can use the FluentFunctions API to write

     Function standardFn = FluentFunctions.ofChecked(this::myMethod);
    

    ofChecked takes a jOOλ CheckedFunction and returns the reference softened back to a standard (unchecked) JDK java.util.function.Function.

    Alternatively you can keep working with the captured function via the FluentFunctions api!

    For example to execute your method, retrying it up to 5 times and logging it's status you can write

      FluentFunctions.ofChecked(this::myMethod)
                     .log(s->log.debug(s),e->log.error(e,e.getMessage())
                     .try(5,1000)
                     .apply("my param");
    

提交回复
热议问题