出现错误时在if/else块之间切换
switch between if/else blocks on error
我正在寻找一种在发生错误时在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
}
我玩过尝试/投掷/接球,但似乎接球必须遵循尝试语句。任何想法/建议都将不胜感激!
当我遇到这种情况时,我用else
块中需要的代码创建了一个单独的函数。然后,只要需要(如果发生错误,并且在else
块中),我就会调用函数。
看起来你不可能有一个"else":
int error = 0;
if( choice==1 ) {
// Something happens
error = 1;
}
if( error == 1 || choice == 2 ) {
// Do things
}
您确实希望将其拆分为两个不同的条件块。毕竟,你并不是说"其他"。
if(choice==1)
{
//if error occurs....
}
if(choice==2 || error)
{
//switch to this block
}
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 我的简单if-else语句是如何无法访问的代码
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 如何删除peer if else分支中的冗长句子
- 我想知道长双倍和双倍之间的区别
- 使用try-Catch异常处理程序和if-else条件检查之间的区别
- 出现错误时在if/else块之间切换