Introspection of exceptions that a function can throw

SealabJaster sealabjaster at gmail.com
Wed Feb 24 19:58:41 UTC 2021


On Wednesday, 24 February 2021 at 19:38:53 UTC, Mark wrote:
> Is there a way to obtain a list, at compile-time, of all the 
> exception types that a function might throw (directly or 
> through a call to another function)?
>
> Thanks.

I don't believe that there's any built-in way, as that'd mean 
that functions would have to define what they throw as part of 
their signature (like in Java).

But you can semi-emulate it with metaprogramming, it's just that 
you'll have to manually keep all the information up to date.

Here's an example: https://run.dlang.io/is/UUHumO

Which supports saying what a function directly throws 
(`@Throws`), and also saying what functions it calls that may 
also throw well-defined exceptions (`@ThrowsInclude`), and 
results in `(Exception, MyExcept)`


More information about the Digitalmars-d-learn mailing list