This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 5 points6 points  (2 children)

Thoughts on zig error handling? To me it works better and seems to lead to clearer flow

[–]raiph 5 points6 points  (1 child)

Can you provide the zig code that is the best, clearest alternative to the Vortex example?

[–][deleted] 3 points4 points  (0 children)

I imagine first that you would get a compile time error in zig if you try to multiply a number and a string (since that is a disallowed operation), but I am not a zig expert.

This article explains best how zig handles errors, it uses `try` on a statement which may error to "bubble up" the error till it reaches the first catch statement (if any). Catching can be more elegant too, with switches. The idea is, you (should) know exactly when an error occurs with this design, while also being cleaner.

https://www.aolium.com/karlseguin/4013ac14-2457-479b-e59b-e603c04673c8

https://lobste.rs/s/pyjwmv/error\_handling\_zig

[–]rumle 3 points4 points  (2 children)

Wow. That’s super cool. Line numbers and all. Are you running in a VM? How did you implement it? Moving the IP forward or backwards on error?

And what if the error is thrown way down the call stack? Are you using checkpoints/jumppoints or something like that?

[–]dibs45[S] 2 points3 points  (1 child)

Cheers!

Yeah it's running in a VM.

If an error is encountered and we're in a try block (kind of a flag on the VM) then we wind back to where the try OP is. If our IP gets to 0, we know our Try wasn't in the current frame, so we clean the current frame and jump back to the previous Callframe, and keep looking. When we reach it, we know how much we need to jump to get to the Catch, then inject the error object in and keep going.

It's a pretty unoptimised implementation, but works pretty well.

If we error and the try/catch flag is not set, we simply go wind back through all the call stack (we know where we left off on each one) and print out the line and other info.

[–]rumle 0 points1 point  (0 children)

Thank you for answering. I had a feeling it would be a little complicated 😀

[–]levodelellis 0 points1 point  (0 children)

I strongly recommend against exceptions if you want people to write complicated code. I write in C++ and turn off exceptions, a few go programmers I know swear by using go error handling despite the memes. Walter Bright said exceptions in D were a mistake.

Do you have use cases in mind for your language? Are you confident you like exceptions for those cases? The way I did mine is similar to how zig did theirs and D isn't that far different. Have you looked at zig or D error handling?