DIY checking exceptions

jag via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 26 17:10:32 PDT 2017


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.


More information about the Digitalmars-d mailing list