How to use exceptions

Adam D Ruppe destructionator at gmail.com
Fri Aug 12 01:00:11 UTC 2022


On Friday, 12 August 2022 at 00:40:48 UTC, H. S. Teoh wrote:
> Hmm! That gets me thinking.  Maybe something like this?

aye. and scopes share dtors which means we can do it in the lib 
today:

---
struct AdditionalInfo {
         static string[] info;

         this(string info) {
                 AdditionalInfo.info ~= info;
         }

         ~this() {
                 AdditionalInfo.info = AdditionalInfo.info[0 .. $ 
- 1];

         }

         @disable this(this);
}

class AdditionalInfoException : Exception {
         this(string t) {
                 import std.string;
                 super(t ~ "\n" ~ AdditionalInfo.info.join(" "));
         }
}

void bar() {
         with(AdditionalInfo("zone 1")) {
                 with(AdditionalInfo("zone 2")) {
                 }

                 throw new AdditionalInfoException("info");
         }
}

void main() {
         bar();
}
---

the throw site needs to cooperate with this but you could still 
try/catch an operation as a whole too and attach the original 
exception in a new one.

needs a bit more thought but this might work. biggest problem is 
still being stringly typed, ugh.

with compiler help tho we could possibly attach info on function 
levels right in the EH metadata, so it looks it up as it does the 
stack trace generation. but that would be limited to per-function 
i believe... but eh there's nested functions.

tbh i think op's delegate is a better plan at this point but 
still my brain is running some concepts.


More information about the Digitalmars-d-learn mailing list