[OffTopic]: Safer usage of C++ in Chrome

rikki cattermole rikki at cattermole.co.nz
Fri Sep 10 00:15:17 UTC 2021


On 10/09/2021 11:57 AM, Walter Bright wrote:
> Momentum seems to be building behind the idea that exception handling is 
> the wrong path.

What I'm interested in atm is weather open-ended exceptions with stack 
unwinding is the wrong way to go.

Super simple question but I am starting to be of the belief that this is 
wrong. We need stronger compiler guarantees with compiling backed checks.

Something like this:

Basically every functions gets attributed with a __throws attribute, 
inferred based upon what it calls, what it catches and what it throws.

Structs only that get moved not copied, with a guarantee that it'll be 
caught before the thread ends and the stack will unwind due to using 
regular return's.

What drew me to this is my colorimetry library. I do need compiler 
guaranteed caught exceptional handling, but I don't want the user to 
wiggle out of it by using return values. The problem is exceptions need 
classes and the runtime handling. But I want it to work with -betterC 
(mixing compiler versions with shared libraries, yeah that won't end well).

With this, I *might* be able to work my way up to a full GUI toolkit. 
This is the big missing piece right now.

```d
struct NullPointerException {
     string message;

     string moduleName;
     int lineNumber;

     this(string message, string moduleName = __MODULE__, int lineNumber 
== __LINE__) {
         this.message = message;
         this.moduleName = moduleName;
         this.lineNumber = lineNumber;
     }
}

void doSomething() /* __throws() */ {
     State* state = newState;

     try {
         state.someComplexProcess();
     } catch (scope ref NullPointerException npe) {
         // ... stuff here
     }

     // scope void[NullPointerException.sizeof + size_t.sizeof] 
__exceptionStorage;
     // state.someComplexProcess(__exceptionStorage[]);
     // if ((cast(size_t*)&__exceptionStorage[0]) == 1 && scope ref 
NullPointerException npe = 
*(cast(NullPointerException*)&__exceptionStorage[size_t.sizeof])) {
     //      // ... stuff here
     // }
}

void someComplexProcess(State* state, /*scope void[] __exceptionStorage 
*/) /* __throws(NullPointerException) */ {
     Process process;

     process.add(libFunc(state, /* __exceptionStorage */));
}

int libFunc(State* state, /*scope void[] __exceptionStorage */) /* 
__throws(NullPointerException) */ {
     if (state is null) {
         throw NullPointerException("State cannot be null");
         // if (__exceptionStorage.length < NullPointerException.sizeof 
+ size_t.sizeof)
         //      assert(0);
         // (cast(size_t*)&__exceptionStorage[0]) = 1;
         // 
*(cast(NullPointerException*)&__exceptionStorage[size_t.sizeof]) = 
NullPointerException("State cannot be null");
         // return ReturnType.init;
     }

     return 0;
}
```


More information about the Digitalmars-d mailing list