#Exception handling
13 messages · Page 1 of 1 (latest)
I'm on mobile and the indention is kinda hard to follow but it doesn't look like the try catch is in the loop. Meaning it would just end, like you're experiencing
Hi, Jam,
There are two main issues with your code:
- If you don't want your program to end if the user enters the number bigger than 2, you need to give it something to do. Note that your program will skip the branch
if (inputuser <= 2)entirely if the input is bigger than 2. That's the reason why the progam ends. What I'd advise is to try and describe your flow in natural language first, using the word "while" or "until". - The
catch(...){...}statement's purpose is to handle any exceptions thrown in thetrypart. So if you want to utilize it, you'd have to throw something from thetrypart, for example:
try
{
throw std::out_of_range("Your argument is out of range");
}
catch (std::exception& e)
{
std::cerr << e.what() << '\n';
}
you have to return to the top
you can use call starters such as a: or b:
and use goto a
or goto b