如何使用 try 和 catch 中止函数

How do you abort from a function using try and catch?

本文关键字:函数 catch 何使用 try      更新时间:2023-10-16
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 子句中指定的类型匹配。在代码中,将输入读取到的变量抛出。这很好,但由于这些变量是字符串,因此您捕获的类型也应该stringcatch (char)子句是合法的,但不会抓住抛出的string,因为它们不是同一类型,即使它们具有相似的值。

提前离开函数的更典型方法是使用 return ,但是如果您的赋值要求您使用异常,那就这样吧。