DIY checking exceptions

Moritz Maxeiner via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 26 18:00:00 PDT 2017


On Tuesday, 27 June 2017 at 00:10:32 UTC, jag wrote:
> On Monday, 26 June 2017 at 21:53:57 UTC, John Colvin wrote:
>>
>> I wonder what could be done with something like this:
>>
>> void foo(int a)
>> {
>>     if (a > 0)
>>         throw new BlahException("blah");
>>     throw new BloopException("bloop");
>> }
>>
>> unittest
>> {
>>     // NEW FEATURE HERE
>>     alias Exceptions = __traits(thrownTypes, foo);
>>     static assert (staticIndexOf!(BlahException, Exceptions)
>> >= 0);
>>     static assert (staticIndexOf!(BloopException, Exceptions)
>> >= 0);
>> }
>>
>> I'm imagining one could use that to do quite a lot of what 
>> checked exceptions provide.
>
> So the only way for a programmer to know what exceptions can be 
> thrown by a method is by running the code? In Java this is 
> known while you are writing the code, even before you compile 
> the code. And the compiler verifies that you are handling or 
> passing on all possible exceptions. This is important.

As I have pointed out, implementing such a feature for function 
exception sets would be *one* component; the others are mentioned 
here [1].

[1] 
http://forum.dlang.org/post/uovtkvpdagzagzhyacbp@forum.dlang.org


More information about the Digitalmars-d mailing list