the question,lambda expression(inside it's body) can throw checked exception or not,it solely depends on the implementation policy of lambda expression i.e.how really are you using or implementing lambda in code,in your case you have used the lambda expression as a method arguments,now seeVicky Roy wrote:Here under main method, 1st statement is compiled and 2nd/3rd can't be compiled. For 3rd statement reason is "The use() method is overloaded to take both Callable and Supplier parameters. The compiler does not take into account the fact that the body of the lambda expression happens to throw an exception; therefore, it does not know how to tell them apart." means compiler not able to understand that this lambda expression is throwing checked exception so it is not able to decide which method to be mapped. Now for 2nd statement the reason is "does not compile, as Supplier does not support checked exceptions.".
Isn't 2nd statement's justification contrary to the justification for line 3rd. 3rd statement is not able to compile because compiler is not able to understand that lambda expression is throwing exception but for 2nd statement, compiler is able to understand that lambda expression is throwing checked exception. how?
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
here the lambda is a Supplier,and it has "get" method where it is not allowed to throw any Exception,though you can handle them inside the lambda body by using try-catch block.mentioned well in Supplier API you can see inside API,in the signature of get method,it is not allowed to throw Exception.
lambda is undefined here so the compiler will get confused to opt b/w the overload method "use",as compiler will fail during parsing the target type of lambda expression(whether it is Callable or Supplier type) used as a method argument.
so finally compiler can understand well that lambda is throwing exception but deny for it if lambda tries to break the policy(for,it is used as).
Hope this will help!
Kind Regards,
Praveen.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
praveen kumaar wrote:vicky,please read the explanation accordingly with the code.i had written a code line first and then explained it,so please read it with this order.
the line lambda is a supplier is for 2nd code snippet i.e., useSupplier() method.
Kind Regards,
Praveen.
use(()->throw new IOException;)
lambda is undefined here so the compiler will get confused to opt b/w the overload method "use",as compiler will fail during parsing the target type of lambda expression(whether it is Callable or Supplier type) used as a method argument.
Because the previous two methods are not overloaded and compiler will easily finds what is the type of lambda from the method signature where you declared them.Vicky Roy wrote:Sorry for misunderstanding. Here my question is rephrased. Why lambda is undefined here? Earlier compiler was easily to map it with Supplier/Callable but why not now?
No worries,it's ok.Vicky Roy wrote:Sorry for misunderstanding.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Vicky Roy wrote:Still I would argue. In case of Use() when compiler checks that it is showing exception, then it should map this call with the method expecting Callable since it only supports checked exceptions!!! That's what happened in case of 1st statement.
Henry Wong wrote:
Vicky Roy wrote:Still I would argue. In case of Use() when compiler checks that it is showing exception, then it should map this call with the method expecting Callable since it only supports checked exceptions!!! That's what happened in case of 1st statement.
The compiler doesn't check for exceptions as part of the signature. And you can't really argue with the compiler -- it will fail to compile every time...
As for the case of the first statement, there was only one option. The compiler did not have to disambiguate the method call.
Henry
Vicky Roy wrote:
Fine. Then lets say I agree what you are saying that compiler doesn't understand the exception and it can't decide appropriate method. But then why useSupplier() is not compiling? Since compiler can't understand the exception, it should easily map and error may come at runtime!!!
Henry Wong wrote:
Vicky Roy wrote:
Fine. Then lets say I agree what you are saying that compiler doesn't understand the exception and it can't decide appropriate method. But then why useSupplier() is not compiling? Since compiler can't understand the exception, it should easily map and error may come at runtime!!!
The compiler does not use the checked exception to resolve overloaded methods. However, once a method is resolved, then it makes sure that checked exceptions are either caught or declared as throws.
Henry
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|