How can I re-throw an exception in a lambda block as from the outer block?

后端 未结 2 552
清酒与你
清酒与你 2021-01-05 18:04

With following code,

void key(Key) throws SomeCheckedException {
}

void supplier(Supplier s) throws Some         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 18:35

    You can't. Supplier#get() does not declare to throw any (checked) exceptions. Remember that a lambda expression simply creates an instance, it doesn't actually invoke the target functional interface method.

    If you want to, you can wrap the checked exception in an unchecked exception and throw that.

提交回复
热议问题