I've ported my language from C to C++ (a story of error handling) #

I've been writing my programming language in pure C for quite some time, but recently I decided to port it to C++. The key problem that made me do so is error handling. While I was working on the bytecode virtual machine, it was all relatively simple. The virtual machine is just a large switch over the opcodes with relatively trivial functions for basic arithmetic operations, jumps and conditions.

As I started to work on the parser and runtime data structures, the code quickly became hard to reason about. This is in part because I decided to gracefully handle memory allocation errors. To understand the issue, let's consider a simple function, assoc_get, which takes an indexable object and returns a value at index:

Value obj = mk_array(10);
Value index = mk_i64(5);
Value val = mk_i64(42);
// Writes "42" at array index 5
assoc_set(obj, index, val)
Value res = assoc_get(obj, index);

Now, there are 2 possible error cases here: