DIY checking exceptions

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 27 00:36:59 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.

No, that's completely the opposite of what I was suggesting. It 
would all be static (i.e. compile-time) introspection.


More information about the Digitalmars-d mailing list