如何使用 try 和 catch 中止函数
How do you abort from a function using try and catch?
void AddNewPerson(string &firstName, string &lastName, string &id)
{
try{
cout <<"Enter first name (a to abort): ";
cin >> firstName;
throw firstName;
cout <<"Enter last name (a to abort): ";
cin >> lastName;
throw lastName;
cout <<"Enter ID Number (a to abort): ";
cin >> id;
throw id;
}
catch(char x){
if(x == 'a'){
//exit here
}
}
}
每当用户想要使用 char 'a' 中止时,我都会尝试退出该函数。 但我不确定如何使用 try 和 catch 语句来做到这一点。有什么帮助吗?
你的意思可能是这样的:
cout <<"Enter first name (a to abort): ";
cin >> firstName;
if (firstName == "a") throw firstName;
没有if
整个事情就没有意义,因为throw
之后的下一行将永远不会被执行。
可能有必要在更广泛的背景下展示发生了什么。假设你有一个多次调用函数的循环:
while (true) // endless loop
{
string firstName, lastName, id;
AddNewPerson(string &firstName, string &lastName, string &id);
...
}
// Do something else
您可以使用异常来打破循环:
try {
while (true) // endless loop
{
string firstName, lastName, id;
AddNewPerson(string &firstName, string &lastName, string &id);
...
}
}
catch(EndOfInput x)
{
// Nothing is actually exceptional here: the user just finished their input
}
// Do something else
如果使用此构造,请按以下方式更改输入函数:
class EndOfInput: public std::exception {};
void AddNewPerson(string &firstName, string &lastName, string &id)
{
cout <<"Enter first name (a to abort): ";
cin >> firstName;
if (firstName == "a")
throw EndOfInput();
cout <<"Enter last name (a to abort): ";
cin >> lastName;
if (lastName == "a")
throw EndOfInput();
cout <<"Enter ID Number (a to abort): ";
cin >> id;
if (id == "a")
throw EndOfInput();
// No need to catch anything here
}
注意:建议抛出std::exception
的子类,而不是像string
这样的一般类,以避免某种混淆。
void AddNewPerson(string &firstName, string &lastName, string &id)
{
try{
cout <<"Enter first name (a to abort): ";
cin >> firstName;
if(firstName == "a")
throw firstName;
cout <<"Enter last name (a to abort): ";
cin >> lastName;
if(lastName== "a")
throw lastName;
cout <<"Enter ID Number (a to abort): ";
cin >> id;
if(id == "a")
throw id;
}
catch(string x){//catch is for exeptions.
if(x == "a"){
//exit here
return;
}
}
}
return
退出函数。不过,我看不出你如何在第一次投掷之后到达线。
正如其他人所说,你应该抛出一个exception
(或子类(的实例。
class abort_exception : public exception{}
//....
void AddNewPerson( string & firstName, string & lastName, string & id )
{
try
{
cout << "Enter first name (a to abort): ";
cin >> firstName;
if( firstName == "a" )
throw abort_exception();
// ...
}
catch( abort_exception& e )
{
cerr << "User Aborted.";
}
}
您不需要从catch
块中return
,因为执行将在块之后继续,该块不执行任何操作,只是返回。您可能希望有一些返回值,让调用方知道用户是否中止。在这种情况下,你会得到这样的东西:
// returns true on success, false on failuer
bool AddNewPerson( string & firstName, string & lastName, string & id )
{
try
{
// ...
}
catch( abort_exception& e )
{
cerr << "User Aborted.";
return false;
}
return true;
}
throw
引发异常,但是如果您想因此离开函数,则应确保在实际退出函数之前不要随后捕获它。不过,这就是您在代码中所做的;你无条件地抛出异常,然后立即测试你抛出的值。
如果您只应该在用户输入特定内容时退出,请确保在启动离开函数的过程之前检查该特殊值。也就是说,在检查值之前不要抛出:
cin >> value;
if (<value-is-special>) {
throw <something>;
}
如果等到捕获异常后再检查值,则为时已晚。一旦你投掷,你就不能回到你投掷的地方并恢复运行,以防用户没有进入特殊的中止指示器。
您的教师可能希望您在函数外部的调用方中捕获异常。根据您在函数外部执行的代码行,您将知道您是否获得了请求的所有信息:
try {
AddNewPerson(...);
// If you get here, you have all the data about a new person.
} catch (<type>) {
// If you get here, the user aborted.
}
您扔出的东西的类型应与 catch
子句中指定的类型匹配。在代码中,将输入读取到的变量抛出。这很好,但由于这些变量是字符串,因此您捕获的类型也应该string
。catch (char)
子句是合法的,但不会抓住抛出的string
,因为它们不是同一类型,即使它们具有相似的值。
提前离开函数的更典型方法是使用 return
,但是如果您的赋值要求您使用异常,那就这样吧。
- 是否有理由大多数/所有 try-catch 示例只对 throw 语句使用 void 子函数
- 构造函数中的异常:init() 方法、指针、大型 try/catch 或
- 函数表达式的字符串转换,带有 Catch 断言中的参数
- std::current_exception 是否应该从类析构函数中的 catch 块返回非空值
- 从catch块调用异常类中的函数并不是打印从try块传递的值
- 处理构造函数C++中的Try-catch块
- 如何使用 try 和 catch 中止函数
- 从catch块中的函数重试
- 函数try catch语法和main
- 我仍然可以在catch块内调用的函数中重新抛出异常吗?
- 尝试在main()函数中使用不带括号的catch块
- mfccdatabase::OpenEx函数如何知道它在try/catch块中
- 我可以写一个类似于简化函数模板的catch子句吗?
- c++中构造函数抛出的Catch错误
- Try/catch块用于在构造函数内部将简单内存分配到公共原始指针
- 对裸函数使用Try/Catch
- 通过模板元编程用try包装任意函数调用.catch块在现代c++中
- 在析构函数中尝试Catch块
- 函数的try-catch语法的区别
- 在c++中使用try和catch方法创建一个函数来获取用户名