r/Compilers • u/ApprehensivePipe1584 • 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.
4
u/WittyStick Oct 29 '26 edited Oct 29 '26
Remove
defer, addfinally.Then use a better tried and tested technique, called
using.The trick is that this just expands to
try/finally, where the resource is disposed in thefinallyblock.This is structured resource management.
deferis unstructured control flow akin togoto(orcomefrom).deferis a tragedy.usingis infinitely better thandefer.3
u/azhder Oct 29 '26
Do you like stack unwind? If not, don’t do it. It’s really up to your preference.
I’m more intrigued why have multiple ways to handle errors in the first place. Can’t there be a single way or at least syntax for it?
2
u/rjmarten Oct 29 '26
What's the difference between multiple returns and tuple returns?
And why do you want both tagged unions and bare unions?
I don't know if I can answer your question before knowing the vision behind all these features.
1
u/Exciting_Clock2807 Oct 29 '26
Multi-return example is really just a poor man’s solution to tagged enums, I would cross it out.
But to answer your question - you came to correct conclusions. Stack unwinding defer is indeed needed for try-catch. But do you need try-catch - that’s for you to decide, as a language designer.
1
u/MitchAlsup Oct 30 '26
"I actually don't have a problem with the idea that handling a error isn't required too much,"
The word is 'often' as in "errors do not happen all that often.
"however the big problem is defer. Because I want
deferto be a zero-cost,"It can never be zero-cost; all you can do is to make the language fool you into thinking is is low cost.
"it does not run during stack unwinding,"
Are you going to run the destructors during stack unwind ??
"and I don't want to put in the effort to make it do so."
Hiding things from the designer is rarely a good idea.
"This is annoying because if your primary error handling is throw, defer is basically useless for error cleanup,"
I see those as 2 orthogonal choices. defer could be as simple as having the compiler find a defer-block in some outer scope, and lob on new deferred items onto the list(s).
Is there any notion of order in these deferrals ? Is there any notion of priority in these deferrals ?
"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.
I suggest you re-visit both concerns until you come up with an easy to use, fast, reliable, and complete error/exception handling method. Do not start by discarding a or b.
2
u/mamcx Oct 29 '26
Your problem is instead what is the composability of all this ways. What happen when is needed to deal with all at once?
2
u/jason-reddit-public Oct 29 '26
Exceptions can be emulated via setting a thread local variable and checking this at return sites which should be similar in performance to returning multiple values go style (or returning an optional). Technically you are unwinding the stack but with less burden than C++ compilers writing out enough metadata to do recovery without extra inline code.
In my C like language I have try_call and throw_error which is not much more than a wrapper around setjmp/longjmp. It's really meant to be the error handling mechanism of last resort (to handle sigsegv etc). I don't have defer or finally (and use the hans boehm gc instead of manual free) so this seems to work ok.
1
u/nzmjx Oct 30 '26
In my language, there is try/catch/throw, result< R, E > and defer. I don't see any contradiction between them.
defer statements are adding expressions to execute while the scope of current block is being closed, whether normally or unwinding.
In my language throw/catch is the canonical/suggested form of exception handling. But as a language designer, I prefer to not choke my preferences to programmers' throat, so there is result data type in the standard library if API designer prefer to use it instead.
Also, to please both camp as much as possible, there is an exception handling alternation syntax. If it is used on an expression which throws, return value is automatically converted to result< ReturnType, BaseExceptionType >. If it is used on an expression which returns result< R, E > and E indeed inherits BaseExceptionType (directly or indirectly), E is thrown on error case.