C++中的例外如何返回"主菜单"

Ofe*_*Ron -1 c++ exception

好吧,我正在学习考试,所以我尽量保持我的代码尽可能简单,但后来发生了一些非常奇怪的事情:练习是改变以下代码,并通过使用例外,返回主菜单.

这是问题的代码:

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;
       sleep(3);
     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
        sleep(3);
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  string answer;

 while (1)
 {

    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer[0])
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    } // switch 

  } // while 

} // mainloop 
Run Code Online (Sandbox Code Playgroud)

而我所做的是:

void ask_return() {
    char c;
    cout << "Return to main menu? y/n:"<<endl;
    cin >> c;
    if (c=='y') throw 1;
}

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;

     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  char answer;

 while (1)
 {

    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer)
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    }
  }

}



int main() {
    try {
        mainloop();
    } catch (...) {
        mainloop();
    }
}
Run Code Online (Sandbox Code Playgroud)

它在开始时工作正常,但之后一次它用未处理的异常消息终止我的程序.为什么?

最简单的正确方法是什么?

编辑:这是一种工作方式:

void ask_return() {
    char c;
    cout << "Return to main menu? y/n:"<<endl;
    cin >> c;
    if (c=='y') throw 1;
}

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;

     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  char answer;

 while (1)
 {
    try {
    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer)
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    }
    } catch (...) {
    }
  }

}



int main() {
    mainloop();
}
Run Code Online (Sandbox Code Playgroud)

Fre*_*Foo 5

我只回答"怎么来"的部分,因为最简单的正确方法就是你应该自己解决的问题.当你这样做

try {
    mainloop();
} catch (...) {
    mainloop();
}
Run Code Online (Sandbox Code Playgroud)

你执行mainloop,捕获任何异常.捕获异常时,处理程序在外部mainloop再次执行.你会想要反复进入,每次捕获异常.trymainloop