www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Introspection of exceptions that a function can throw

reply Mark <smarksc gmail.com> writes:
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.
Feb 24 2021
next sibling parent reply SealabJaster <sealabjaster gmail.com> writes:
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)`
Feb 24 2021
parent SealabJaster <sealabjaster gmail.com> writes:
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.
Feb 24 2021
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
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
Feb 24 2021
prev sibling parent reply James Blachly <james.blachly gmail.com> writes:
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?
Feb 25 2021
parent Arafel <er.krali gmail.com> writes:
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?
Feb 25 2021