Throwing from a lambda isn't supported by the compiler

Andre Pany andre at s-e-a-p.de
Mon Sep 9 10:32:54 UTC 2019


On Monday, 9 September 2019 at 09:37:25 UTC, a11e99z wrote:
> On Monday, 9 September 2019 at 09:14:08 UTC, Andre Pany wrote:
>> I noticed, that I cannot throw from the lambda expression:
>>   Error: expression expected, not throw
>> but have to use the longer syntax () {throw new Exception();}
>>
>> ---
>> void foo(void function() e){}
>>
>> void main()
>> {
>>     foo(
>>         () => throw new Exception()
>>     );
>> }
>> ---
>>
>> The shorter lambda syntax would be more readable in my case.
>> Is there a reason why it isn't allowed or should I create an
>> enhancement request?
>>
>
> and next code too. C# code good is it
> int func( bool ok ) {
>     return ok ? 123 : throw new Exception( "oops 123");
> }
>
> cuz throwing exception ever is not compatible with any return 
> type so I dont see reasons do not allow such expressions

Thanks for the answers.
I will create an enhancement requests and add the C# example.

Kind regards
André


More information about the Digitalmars-d-learn mailing list