5 ms·
> Exiting a function immediately in case a function call fails sounds exactly like an exception. No, this would be an early return. The specificity of exceptio
by Zababa 5y ago
> Exiting a function immediately in case a function call fails sounds exactly like an exception.
No, this would be an early return. The specificity of exceptions is that their default behavior is to bubble up through the entire stack. try in Zig only bubbles up to the calling function.
- hota_mazi 5y agoAh, ok. So... you have to do the bubbling up manually if the caller cannot handle the early exit. Maybe I'm missing something but it still feels like a solution that looks like exception but is inferior to them.
- Zababa 5y agoI think you are missing something, at least on the "ideological" aspect of things: it's part of the values/guarantees of Zig that all control flow is going to be explicit. On the landing page of Zig (https://ziglang.org/ https://ziglang.org/), "No hidden control flow." is the first bullet point. So "try" is the way they found to make control flow explicit, but also less painful that if (...) { return something; }. It's also a solution that's checked by the type system, exceptions usually aren't. I don't know if it's the best solution, but I can understand where they come from.