I am trying to write a function that prompts a user for a five digit number, and I want to write an exception block to handle bad input incase the user tries to enter a string or some non-integer input.
I know how to write an exception handling block for something like a division function where you thrown an exception for the denominator being 0, but I have no clue how to do this for input that I have no control over.
First of all, I’d generally advise against this — bad input from the user is nearly the rule rather than the exception.
If you insist on doing it anyway, you’d probably do it by accepting anything as input, and then throwing an exception when/if it’s not what you want:
I repeat, however, that I don’t think this is generally a good idea. I’d note (for one example) that iostreams already define a
failbit specifically for situations like this (and the normaloperator>>to read an int uses that mechanism).