exceptions, try/catch/throw

hi guys i'm pretty new to c++ and am having trouble with this problem. in my main method I try to call a function which will check to see if a certain string matches any of the strings within an array called products.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <exception>
//#include <new>
#include "MyException.h"
#include "MyException.cpp"

using namespace std;

int main()
{
int productIds[]= {4, 5, 8, 10, 13};
string products[] = { "computer", "flash drive", "mouse", "printer", "camera" };

try{
    getProductID(productIds, products, 5, "mouse");
}
catch(exception & e){
    cerr << "Exception caught: " << e.what() <<endl;
}
try{
    getProductID(productIds, products, 5, "camera");
}
catch(exception & e){
    cerr << "Exception caught: " << e.what() <<endl;
}
try{
    getProductID(productIds, products, 5, "laptop");
}
catch(exception & e){
    cerr << "Exception caught: " << e.what() <<endl;
}

return 1;

}



here is the MyException.cpp file which has the getProductID function that I thought would throw an exception if the array does not contain the string (last argument for example "laptop"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "MyException.h"
#include <string>
#include <exception>
using namespace std;


int getProductID(int ids[], string names[], int numProducts, string target)throw(MyException){

    MyException nothing;
    bool found = false;

    for (int i=0; i < numProducts; i++){
        found = false; //reset found if
        if (names[i] == target){
        found = true;
        return ids[i];
}
}

    if(found == false){
        throw nothing;
    }

    return 1;
    }


finally here is the header file itself which has the exception

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MYEXCEPTION_H
#define MYEXCEPTION_H
#include <stdexcept>
#include <iostream>
#include <string>
#include <exception>

using namespace std;

class MyException: public exception
{
    public:
              virtual const char* what() const throw()
              {
                return "Caught a value not found exception!";
              }
};

#endif  //MYEXCEPTION_H 


unless I use #include "MyException.cpp" I get an error saying that getProductID was not declared in the scope. when I do include "MyException.cpp" I get an error saying "multiple definition of 'getProductID(int*, std::string*, int*, std::string)'"

Am I not doing my try/catch sequence right? is my getProductID function incorrect? Any feedback or help would be really appreciated, thank you.
1) Never #include cpp files.

2) Your getProductID function needs to be prototyped somewhere so that main.cpp can see it. This means either in main.cpp itself, or in MyException.h (putting it in the header makes more sense)

3) Putting a try/catch around every single function call kind of defeats the entire point of exceptions. Consider condensing that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// I added indentation here.  Indentation is your friend

int main()
{
    int productIds[]= {4, 5, 8, 10, 13};
    string products[] = { "computer", "flash drive", "mouse", "printer", "camera" };

    try
    {
        getProductID(productIds, products, 5, "mouse");
        getProductID(productIds, products, 5, "camera");
        getProductID(productIds, products, 5, "laptop");
    }
    catch(exception& e)
    {
        cerr << "Exception caught: " << e.what() <<endl;
    }

    return 0;  // you generally want to return 0 from main, as that indicates the program
       // ran successfully.  Any nonzero value means it did not run correctly/successfully.
}
thanks this is all really helpful. What exactly do you mean by prototyped? do you mean that I can just use the #include "MyException.cpp" code in the header file? I understand the concept that it must be accessible by main but I'm not sure what you mean by prototyping it, could you maybe explain?

thanks again
What exactly do you mean by prototyped?


A prototype is basically the declaration of a function without giving it a body.

So putting this in your header would suffice:

 
int getProductID(int ids[], string names[], int numProducts, string target)throw(MyException);


(also the exception qualifier there in the function name is not necessary -- you should probably remove it -- I believe that "feature" was deprecated because it was stupid and no compilers took advantage of it)

do you mean that I can just use the #include "MyException.cpp" code in the header file?


No no. Never ever ever #include a cpp file.

Thanks this works but the only problem now is that I have to return the product ID if there is a matching word of what they are searching. This doesn't seem to work with my current code and I don't believe it is an error with my .cpp file. Would I need to add something in my try/catch to allow the returning of the right index and not an exception?
nevermind got it, thank you disch
Topic archived. No new replies allowed.