Amir
Amir

Reputation: 69

How to use try catch efficiently in C++

I am a beginner in C++, so help me with this logic,

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.

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

Upvotes: 1

Views: 398

Answers (2)

petersohn
petersohn

Reputation: 11730

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. 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).

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.

Upvotes: 2

Jarod42
Jarod42

Reputation: 217775

Once you have !(std::cin >> ncustomerid)

the error state of std::cin is set, you have to reset it before reading from it.

Upvotes: 1

Related Questions