我想知道编写异常收件箱和发件箱是否会改变特定程序的行为,例如throw MyException(); 并抛出(MyException());
我的代码:
#include <iostream>
#include <exception>
using namespace std;
class MyException: public exception {
public:
virtual const char* what() const throw()
{
return "Something bad happened";
}
};
class Test
{
public:
void goWrong()
{
throw (MyException());
}
};
int main()
{
Test test;
try
{
test.goWrong();
}
catch (MyException &err)
{
cout << "The Exception is Executed: " << err.what() << '\n';
}
cout << "Still Running" << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想知道通过使用转换string为使其与 switch 一起工作是否不会影响程序的行为。我使用字符串是因为我正在通过为此方法使用 asci 表来验证用户输入,但未包含在此代码中以使其简短。intstoi>= 48 && <= 57
编码:
do
{
cout << "Choice: ";
string userChoice;
cin >> userChoice;
isValid = validNum(userChoice);
if(isValid)
{
int intUserchoice = stoi (userChoice);
switch(intUserchoice)
{
case 1:
ServerStart();
}
}
}while (!isValid);
Run Code Online (Sandbox Code Playgroud)