Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

c++ - The sample code provided returns a random number, even after throwing an exception (code provided)

I have a sample code to modify and throw exception handling. The problem is even after I threw an exception, the code still returns a random 0. I have spent some time trying to figure out why I still have a 0 returned but I could not find the answer. Does anyone have an idea why the code behaves like this?

#include <stdexcept>
#include <iostream>
#include <string>

using namespace std;


struct myException_Product_Not_Found : exception 
{
     virtual const char* what() const throw() {
        return "Product not found";
     }
} myExcept_Prod_Not_Found;  

int getProductID(int ids[], string names[], int numProducts, string target) {
    for (int i=0; i<numProducts; i++)  {
       if(names[i] == target)
            return ids[i];          
    } 
    try {
       throw myExcept_Prod_Not_Found;   
    }
    catch (exception& e) {
       cout<<e.what()<<endl;     
    }                                       
}

// Sample code to test the getProductID function
int main() {
    int    productIds[] = {4,5,8,10,13};
    string products[]   = {"computer","flash drive","mouse","printer","camera"};

    cout << getProductID(productIds, products, 5, "computer") << endl;
    cout << getProductID(productIds, products, 5, "laptop") << endl;
    cout << getProductID(productIds, products, 5, "printer") << endl;

    return 0;
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

getProductID doesn't throw an exception. You catch the exception you do throw before getProductID has a chance to throw it. As such, you return ... well, nothing. The functions ends without you calling return.

If you had turned on your compiler's warnings* (as should should be doing), the compiler should warn with a message like control reaches end of non-void function. g++ appears to return zero in this instance, but returning zero is probably undefined behaviour.

If you want a function to throw an exception, don't catch the exception you've thrown inside of the function. Move the catch to the outside.

int getProductID(...) {
   ...
   throw myExcept_Prod_Not_Found;
}

string product = "computer";
try {
   cout << getProductID(productIds, products, 5, product) << endl;
} catch (exception& e) {
   cout << "Can't find product id for " << product << ": " << e.what() << endl;
}

* — To turn on warnings in g++, -Wall is a good starting point. @Tomalak Geret'kal suggests -Wall -Wextra -std=c++98 -pedantic or -Wall -Wextra -std=c++0x -pedantic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...