#Handling errors

1 messages Β· Page 1 of 1 (latest)

errant leaf
#

Hi! I'm trying out Advent of Code in Zig, starting on day one πŸ™‚

When using parseInt in a string I should sometimes get an error (which is a good thing!) however I want to use the error to continue the loop i'm currently running. How should I go about it?

#

Using orelse undefined and then checking if var is undefined could work? πŸ€”

#

So If i get an error, I continue. Otherwise I use the parsed integer in the rest of the code.

scenic creek
#

undefined is not something you can compare against, it's a meaningless value

#

std.fmt.parseInt(...) catch continue;

errant leaf
scenic creek
#

you can but it absolutely will not do what you want and it's never something that should be done

#

I think it will be disallowed soon as well

errant leaf
scenic creek
#

catch and try are exclusive so just remove the try

errant leaf
#

ahh ok, makes sense

#

Yep, that worked, just need to fix some types mismatches and should be working πŸ™‚

#

Thanks!

scenic creek
#

np

errant leaf
scenic creek
#

yeah just add curly brackets after catch

simple widget
#

so comparing undefined to undefined is like comparing 'whatever happens to already be here' with 'whatever already happens to be there'

which doesnt not make much sense

#

just felt that this part deserved a bit more detail :D

errant leaf
errant leaf
scenic creek
#

in the sense that the memory you get from malloc is undefined but theyre 2 very different things

#

zigs var foo: i32 = undefined; is effectively equivalent to Cs int foo;, nothing to do with memory allocation

errant leaf
scenic creek
#

technically yes but its rare that someone refers to that as "allocation"

#

allocation usually is used to refer to dynamic allocation e.g. malloc

vagrant dome
#

the heap and the stack are (generally) two very different regions of the address space

#

allocating on the stack is about as expensive as incrementing a pointer (as in, the stack pointer)