Thread overview
Introspection of exceptions that a function can throw
Feb 24, 2021
Mark
Feb 24, 2021
SealabJaster
Feb 24, 2021
SealabJaster
Feb 24, 2021
Dennis
Feb 26, 2021
James Blachly
Feb 26, 2021
Arafel
February 24, 2021
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.
February 24, 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)`
February 24, 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)?

No, since this is not known at compile-time.

See: https://forum.dlang.org/post/qnarroejixxtqxjiwvhy@forum.dlang.org
February 24, 2021
On Wednesday, 24 February 2021 at 19:58:41 UTC, SealabJaster wrote:
> ...

Just realised that can include duplicates, but there's std.meta.NoDuplicates to solve that.
February 25, 2021
On 2/24/21 2:38 PM, 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.

Crazy idea:

Could a program import its own source file as a string (`string source = import('thisfile.d')`) and `-J<path>` , then use a lexer/parser to generate AST of the source code and extract exceptions potentially thrown by given functions -- all at compile  time?
February 26, 2021
You'd hit a very big wall with separate compilation unless you can inspect all the code, and know where to find it.

But you'd have a problem, for instance, if you are writing a plugin (.so / DLL) for a product for which you only have .di files.

Or even worse the other way round: if you want to allow people to write plugins for your product, you can't know what they'll throw, even if they have your code, unless you enforce a `nothrow` interface.

But I guess that if you're not doing any of this, it should be possible... although I'd still do it as a separate pre-compilation step, so it could be cached.

On 26/2/21 3:21, James Blachly wrote:
> On 2/24/21 2:38 PM, 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.
> 
> Crazy idea:
> 
> Could a program import its own source file as a string (`string source = import('thisfile.d')`) and `-J<path>` , then use a lexer/parser to generate AST of the source code and extract exceptions potentially thrown by given functions -- all at compileĀ  time?