Java 8 Lambda function that throws exception?

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

    Several of the offered solutions use a generic argument of E to pass in the type of the exception which gets thrown.

    Take that one step further, and rather than passing in the type of the exception, pass in a Consumer of the type of exception, as in...

    Consumer
    

    You might create several re-usable variations of Consumer which would cover the common exception handling needs of your application.

提交回复
热议问题