What is the difference between these constructs such that one won't compile?

佐手、 提交于 2019-12-08 18:05:26

Your initial attempt at wrapConsumer didn't work because it still took a Consumer as a parameter, and your lambda expression that you attempted to wrap still threw an Exception -- a checked exception. Your try/catch is too far from the thrown exception, because at the point you create a lambda expression, you've created a Consumer there, and Consumer's accept method isn't declared to throw any checked exceptions.

The change to accepting a ConsumerWrapper works because that interface's method declaration allows it to throw Exception, with throws Exception. That allows you to create a lambda expression that throws a checked exception.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!