r/Compilers • • Oct 29 '26

Try/Catch: Yes or No

My language, QuarticC, has many ways to error handle.
Unions:

type Err = DivByZeroErr | int;  
Err divide(int lhs, int rhs) {  
    if (rhs == 0) return DivByZeroErr();  
    return lhs / rhs;  
}  
// Or generic:
type Err<T, E> = T | E;
Err<DivByZeroErr, int> divide...

Tagged Enums:

enum Result<T, E> {  
    Err(E);  
    Ok(T);  
}  
Result<int, DivByZeroErr> divide(int lhs, int rhs) {
    if (rhs == 0) return Result<int, DivByZeroErr>.Err(DivByZeroError());
    return Result<int, DivByZeroErr>.Ok(lhs / rhs);
// Or you could make it non generic.

Multi-Return (assuming your error types have a "ok" state):

DivByZeroErr, int divide(int lhs, int rhs) {
    if (rhs == 0) return DivByZeroErr(), -1;
    return DivByZeroErr(true) /* assuming true = ok */, lhs / rhs;
}

Tuples (same as above, but returning tuples):

(DivByZeroErr, int) divide...

And the problem, try/catch.

int divide(int lhs, int rhs) {
    if (rhs == 0) throw DivByZeroErr();
    return lhs/rhs;
}

I actually don't have a problem with the idea that handling a error isn't required too much, however the big problem is defer. Because I want defer to be a zero-cost, it does not run during stack unwinding, and I don't want to put in the effort to make it do so. This is annoying because if your primary error handling is throw, defer is basically useless for error cleanup, which is it's most useful purpose, and it's not like I'm loosing my main error handling, other forms work fine. So the question is: do I remove try catch, or do I make defer stack-unwinding.

7 Upvotes

27 comments sorted by

View all comments

Show parent comment

0

u/ApprehensivePipe1584 Oct 29 '26 edited Oct 29 '26

I'm absolutely not removing defer. defer is a feature i am keeping as it allows so much code removal for early returns, breaks, etc, adding finally to try/catch is a pain and something i don't want to add, and goto isn't neccesarily bad. My language is not RAII, and doesn't really do that or automatic cleanup and thus using doesn't really fit my language. I probably will be removing try-catch.

3

u/WittyStick Oct 29 '26

You don't actually need finally if you remove all unstructured control flow so that nothing can accidentally cause whatever is in using to leak.

Eg, you can use this trick in C:

 #define using (ptr, acquire, release) \
     for (auto ptr = (acquire); ptr; ptr = ((release), NULL))

Usage:

 int main () {
     using (ptr, malloc(100), free(ptr)) {
         // do something with ptr
     }

     return 0;
 }

This works because the for loop only runs once, so the secondary block of the using is actually just a secondary block in a run-only-once for loop (which the compiler can detect and optimize it away).

However, the caveat to this approach is unstructured control flow: goto, break, continue, return - if you use these inside the secondary block then the resource may not be released. This is something that you can trivially fix if you control the language though - you can just restrict unstructured control flow from being used in such context.

Structured resource handling (using) is better than unstructured resource handling (via defer).

Just like structured looping (for/while) is better than unstructured looping (via goto).

0

u/ApprehensivePipe1584 Oct 29 '26 edited Oct 29 '26

I have never seen a systems language with using, and Zig's whole cleanup mechanism is `defer`, C's full cleanup mechanism is `goto`. If your language is fully manual with no forced cleanups, `using` doesn't make much sense, and with `using`, it's still useless a lot of the time because it doesn't fulfill it's purpose. Cleanup. If you exit a using block other than a normal scope exit, it failed it's purpose. `defer` is not meant to just cleanup a resource at the end of the block, it's ment to stop you from writing a line of cleanup code like `free(ptr)` every exit. `using` doesn't fix that at all. It just has you write the same line you write once at the end at the start, and restricts everything you do in that block. they are entirely different mechanisms for different purposes. For example, this code:

...Vector code...

string loadConfigItems() {

int fd = `open("config.txt", "r");

defer `close(fd);

Vector<string> vec = Vector<string>();
defer vec._destroy();

char[1024] buf;
int last_read;

while (last_read) {
// formatting checking...
char *copy = `malloc((sizeof char) * last_read);
if (!copy) return "";
`memcpy(copy, buf, last_read);

defer `free(copy);

vec.push(copy);
last_read = `read(fd, buf, 1024);

}

return list;

}

Then without defer you would need to
string loadConfigItems() {

int fd = `open("config.txt", "r");

Vector<string> vec = Vector<string>();

char[1024] buf;
int last_read;

while (last_read) {
// formatting checking... manual exiting on every control flow.
char *copy = `malloc((sizeof char) * last_read);
if (!copy) {
`close(fd);
vec._destroy();
return "";
}
`memcpy(copy, buf, last_read);

vec.push(copy);
last_read = `read(fd, buf, 1024);
`close(fd);
vec._destroy();

`free(copy);
}

`close(fd);
vec._destroy();
return list;

}
where would be entirely useless.

0

u/ApprehensivePipe1584 Oct 29 '26

More importantly, defer isn't unstructured control flow. It isn't even control flow. It helps manage unstructured control flow.defer doesn't introduce arbitrary jumps or alter where execution proceeds. It performs a cleanup action that runs when the scope is exited. return, break, continue, goto, or whatever other control-flow mechanism causes the scope to be exited is still the thing determining control flow. The whole point is that it lets you write cleanup once instead of having to duplicate it across every possible exit path.

Calling defer "unstructured control flow akin to goto" is just categorically incorrect.

-1

u/ApprehensivePipe1584 Oct 29 '26

Once again, i'm not removing unstructured control flow and that is something stupid to remove, and also structure resource handlings is once again something that doesn't work because how many ways resources can be created, and goto's purpose isn't for looping, it's for cleanup just like defer. using is better for some form of RAII or rusty language with some set cleanup, not a language with direct control. Using is severely limiting. You can't use any control flow keywords in it for the reason of it doesn't release despite there not being a one shot release in the first place, meanwhile defer is not limiting. There is no "better". They are for different things and different paradigms. Nothing is inherently bad, such as structured control flow, it is just for entirely different types of languages. using is a bad decision for my type of language. Once again. Structured resource handling MAY be better in a simple scripting language or a systems language that does a lot of cleanup for you like C++ (if you define destructors) and Rust. And even then it's not necessarily better. It's almost never better in a systems language that leaves you to your own devices.