私は何度か検索しましたが、私はちょうど私が欲しかったものを見つけませんでした。例外があってもプログラムを続行したい
私はこの障害を見つけた例外的な処理(try/catch)を試していました。プログラムが例外を検出した場合は、何も終了します。
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++;
}
}
};
はこちら機能(私はdebuggにしようとしていますとそれは不完全です)
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
部分ですが、まだプログラムが終了しました。または、他のソリューション/方法/ロジックがありますか? ありがとう!
'catch(...)'を試しましたか?あなたはなぜそれが機能していないかを理解するのを助けたいなら、[mcve]を提供しなければならないでしょう。 – NathanOliver
はい私は 'キャッチ'をしました、それは動作しますが、私が探しているのは、 'キャッチ 'に到達してもプログを続ける方法です。 – LoneRanger17
あなたの質問にはうまくいかない部分が実際に含まれているはずです。 –