【发布时间】:2012-03-26 10:56:04
【问题描述】:
我正在寻找一种在发生错误时在 if 和 else 块之间切换的方法。例如:
cout << "Enter 1 or 2 as your choice...";
cin >> choice;
if(choice==1) {
//do something here
//if error occurs....
} else if(choice==2) {
//switch to this else block
}
我玩过 try/throw/catch,但似乎 catch 必须遵循 try 语句。任何想法/建议将不胜感激!
【问题讨论】:
-
您可以将
else if部分的主体放在一个函数中,然后从两个地方调用它
标签: c++ exception-handling error-handling try-catch