简体   繁体   English

如何在C ++中有效地使用try catch

[英]How to use try catch efficiently in C++

I am a beginner in C++, so help me with this logic, 我是C ++的初学者,所以请帮我解释一下这个逻辑,

I have created a map and entered the data. 我创建了一个地图并输入了数据。 This question is based on Exception handling. 此问题基于异常处理。 If there is error in my try{...} eg. 如果我的尝试中有错误{...},例如。 Wrong data type input, it throwing to catch() and executes mContinueOrNot() function, but the program terminates without getting value for cContinueCharacter. 错误的数据类型输入,它抛出catch()并执行mContinueOrNot()函数,但程序终止而没有获取cContinueCharacter的值。

void mSearchForCustomer()
{
    try
    {
           int ncustomerid;
        std::cout<< "\nEnter the Customer ID to Search \t";
        if(!(std::cin >> ncustomerid))
        {
            throw (ncustomerid);
        }
             /*My Code*/

        mContinueOrNot();
    }
    catch(int)
    {
        std::cout<< "\nWRONG INPUT\n";
        mContinueOrNot();
    }
}




void mContinueOrNot()
{
    char cContinueCharacter;
    std::cout<<"\nEnter 'Y' to continue \n";
    std::cout<<"Continue????????? :\t";
    std::cin>>cContinueCharacter;

    if(cContinueCharacter == 'y' || cContinueCharacter == 'Y')
        mChoice();
    else
        exit(0);

}

Thanks in Advance 提前致谢

You don't need to use exceptions in this case. 在这种情况下,您不需要使用异常。 It's much simpler to do it like this: 这样做要简单得多:

if(!(std::cin >> ncustomerid)) {
    std::cout<< "\nWRONG INPUT\n";
} else {
    std::cout  << "\nSeatch Result:\n";
    ...
}
mContinueOrNot();

Also, throwing an int is generally a bad idea. 另外,抛出int通常是一个坏主意。 You should normally throw only objects that are derived from std::exception for error handling, or an object that does not derive from it for some special cases (but you probably won't need that). 通常,您应该只抛出从std::exception派生的对象以进行错误处理,或者std::exception一些不是从某些特殊情况派生的对象(但您可能不需要它)。

You usually want to use exceptions for more complicated error handling. 您通常希望使用异常来处理更复杂的错误。 Typically you try on some code that can fail at several points, then catch the error and handle it at one point. 通常,您尝试一些可能在几个点失败的代码,然后捕获错误并在一个点处理它。 For example, if a code would look like this without exceptions: 例如,如果代码看起来像这样没有例外:

int innerFunction1() {
    ...
    if (somethingWentWrong()) {
        return SOME_ERROR;
    }
    ...
    return SUCCESS;
}

int innerFunction2() {
   ...
}

...

int outerFunction1() {
    int errorCode;
    if ((errorCode = innerFunction1()) != SUCCESS) {
        return errorCode;
    }
    if ((errorCode = innerFunction2()) != SUCCESS) {
        return errorCode;
    }
    ...
}

int outerFunction2() {
     int errorCode;
     if ((errorCode = innerFunction3()) != SUCCESS) {
        handleErrorInInnerFunction3(errorCode);
        return errorCode;
    }
    ...
}

...

int main() {
    if (outerFunction1() != SUCCESS) {
        handleError();
    }
    if (outerFunction2() != SUCCESS) {
        handleError();
    }
    ...
}

The same would look like this with exceptions: 除了例外,它们看起来像这样:

class SomeException : public std::exception {
    ...
};

void innerFunction1() {
    ...
    if (somethingWentWrong()) {
        throw SomeException();
    }
    ...
}

int innerFunction2() {
   ...
}

...

int outerFunction1() {
    innerFunction1();
    innerFunction2();
}

int outerFunction2() {
     try {
         innerFunction3();
     catch (SomeException& e) {
        handleErrorInInnerFunction3(e);
        throw;
    }
    ...
}

...

int main() {
    try {
        outerFunction1();
        outerFunction2();
    } catch (SomeException&) {
        handleError();
    }
    ...
}

You can probably see why the second example is clearer than the first one. 您可能会看到为什么第二个示例比第一个示例更清晰。

Once you have !(std::cin >> ncustomerid) 一旦你有!(std::cin >> ncustomerid)

the error state of std::cin is set, you have to reset it before reading from it. 设置std :: cin的错误状态,你必须先重置它才能读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM