Java 8 Lambda function that throws exception?

后端 未结 26 1714
臣服心动
臣服心动 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:19

    I think Durian's Errors class combines many of the pros of the various suggestions above.

    • Wrap a throwing function to a standard Java 8 functional interface.
    • Easily specify various policies for handling errors
    • When wrapping a method that returns a value, there is an important distinction between specifying a default value or rethrowing a RuntimeException.
    • Throwing versions of Java 8's functional interfaces
      • Similar to fge's answer
    • Standard interfaces for throwing specific exceptions
      • Which addresses Zoltán's concern

    To include Durian in your project, you can either:

    • grab it from jcenter or maven central at com.diffplug.durian:durian:3.3.0
    • or just copy paste just two small classes into your code: Throwing.java and Errors.java

提交回复
热议问题