我搜索了几次,但我有点,没找到我想要的东西。
我正在进行特殊处理(尝试/捕获),在那里我发现了这个障碍。如果程序发现异常,它就会终止。
我尝试在catch
部分调用一个函数,但它仍然会终止。
void exception_handle() //This is for handling exception if user inputs a char instead of int//
{
user_play uplay;
try
{
uplay.usersentry();
}
catch(std::runtime_error& e)
{
cout<<"Input a string bro, not a character"<<endl;
user_input();
}
}
这是班级:
class user_play //this class is for letting user play the game by allowing them to enter desired number in the desired empty space//
{
public:
void usersentry()
{
int tempdata;
retry:
cout<<"\n\n Enter the row and coloumn where you want to enter data"<<endl;
cin>>i>>j;
if (i>=1 && i<=9 && j>=1 && j<=9)
{
cout<<"\n Enter your desired value to put in that place"<<endl;
cin>>tempdata;
if(tempdata>=1 && tempdata<=9)
{
data=tempdata;
}
else
{
throw std::runtime_error("Soduku contains numbers from 1 to 9 only.Please try again");
loops++;
}
}
else
{
throw std::runtime_error("Soduku row exists between 1 and 9 only.Please try again");
loops++;
}
}
};
这是函数(因为我试图调试它不完整)
int user_input() //this one is for taking correct value from user and storing it in its respective place//
{
a=0;
//Object Declaration//
rowrules rr;
columnrules cr;
//for handling the program exceptions
exception_handle();
//rules for row and column
//rr.rrules();
//cr.crules();
//ruleselect();
//i--;
//j--;
if(a==0)
{
soduku[i-1][j-1]=data;
return soduku[i-1][j-1];
}
else
{
user_input();
}
}
在这里,你看到我试图调用catch
部分中的函数但仍然程序终止。我遗漏了一些基本的东西?或者还有其他解决方案/方法/逻辑吗?
谢谢!
答案 0 :(得分:0)
无法从抛出C ++异常的位置继续执行。 C ++异常不是为此而设计的。但是,如果发生异常,可以重复您想要重复的代码:
for (bool success = false; !success; )
{
try
{
<some code that should be repeated if exception happens>
success = true;
}
catch (...)
{
}
}
请注意,一般情况下,如果发生异常,绝对不做任何事情。如果有的话,至少在某些日志文件中写下来。