catching exception but skipping an iteration
my code works fine other than when the user doesnt input a number and my exception error prints it skips to the next number instead of asking them to reinput it
4 Replies
Well, the
i
in loop still increases, doesn't it
Also, probably a better idea to use int.TryParse()
rather than catching an exception of int.Parse()
In general, exceptions should not be used for control flowi just added i = i -1
to the end of my catch
is that ok
Sure
Protip:
x = x + 1
is just x += 1
is just x++
x = x - 1
is just x -= 1
is just x--
x = x + 2
is just x += 2
and so onfor the sake of readability i would make the input validation a separate loop
that will also help you later when you decide to factor out the input code into another method