#Exception handling

13 messages · Page 1 of 1 (latest)

lone star
#

Hi im trying to make it where this code output this msg to say if the user enters the wrong number but it just ends the code from running. I cannot upload the entire code but I use try{

}catch(...){
cout<<"Enter 1 or 2 to have access to the menu..";
}

#

Is there something wrong where I use catch?

peak torrent
#

Showing the full code would be helpful

lone star
peak torrent
#

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

lone star
#

I changed it

#

Im getting the ame issue

shrewd epoch
#

Hi, Jam,

There are two main issues with your code:

  1. 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".
  2. The catch(...){...} statement's purpose is to handle any exceptions thrown in the try part. So if you want to utilize it, you'd have to throw something from the try part, for example:
try
{
  throw std::out_of_range("Your argument is out of range");
}
catch (std::exception& e)
{
  std::cerr << e.what() << '\n';
}
hearty tree
#

you can use call starters such as a: or b:

#

and use goto a

#

or goto b