try throw catch program

Nov 9, 2013 at 1:35pm
I need to alter my program to use the try throw catch: Here are the bsic instructions for the problem:
Rewrite the program so that it throws an appropriate exception when a product is not found instead of returning a -1.

This is what I have so far (i was attempting to 'throw' inside the function or should I code the try thorw catch in main?):

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
36
37
38
39
40
  #include<iostream>
  #include<string>
 
  using namespace std;
 
  int getProductID(int ids[], string names[], int numProducts, string target)throw(target)
  {
          for(int i=0; i< numProducts; i++)
          {
                  if (names[1] != target)
                     throw(target);
 
                  return ids[i];
          }
  }
 
  int main() //Sample code to test the getProductID function
  {
     int productIds[] = {4,5,8,10,13};
     string products[] = {"computer", "flash drive","mouse","printer","camera"};
 
     try
     {
     cout<< getProductID(productIds, products, 5, "mouse")<<endl;
     cout<< getProductID(productIds, products, 5, "camera")<<endl;
     cout<<getProductID(productIds, products, 5, "laptop")<<endl;
     }
     catch(target)
     {
          cout<<"Error: Product not found."<< endl;
          cout<<"End of program."<<endl;
 
          return 0;
     }
 
 
     return 0;
 
  }


so the errors are all with the function not listing a type with the throw code etc. Anyone have advice on how to best implement the 'try-throw-catch' for this program?

Thanks!
Nov 9, 2013 at 1:46pm
target is not declared in that scope and some syntax is wrong, since you throw a string exception, you must catch that using string also :

1
2
3
4
5
6
7
catch(string str)
     {
          cout<<"Error: Product not found."<< endl;
          cout<<"End of program."<<endl;
 
          return 0;
     }

the message exception is assigned to str so you can also print that :
cout<<"Error: " << str << " Product not found."<< endl;

which when i try produce :
Error: mouse Product not found.
( so, something wrong again w/ the code )

~~~~~~~~~~~~~~~
and your function syntax is wrong :
 
int getProductID(int ids[], string names[], int numProducts, string target)throw(target)


remove throw(target) and that should compile
Last edited on Nov 9, 2013 at 1:55pm
Nov 9, 2013 at 2:11pm
thanks it compiles. but it doesn't run as expected. It is only supposed to throw when the product is not found.

But 'mouse' is a 'found' product (based on my string products and shouldn't be thrown.

actually in my test program the 'laptop' product should have been thrown.......
here is the revised program that is returning 'mouse' as not found (but it is laptop that shouldnt have been found....

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
36
37
38
39
40
 #include<iostream>
  #include<string>
 
  using namespace std;
 
  int getProductID(int ids[], string names[], int numProducts, string target)
  {
          for(int i=0; i< numProducts; i++)
          {
                  if (names[1] != target)
                     throw (target);
 
                  return ids[i];
          }
  }
 
  int main() //Sample code to test the getProductID function
  {
     int productIds[] = {4,5,8,10,13};
     string products[] = {"computer", "flash drive","mouse","printer","camera"};
 
     try
     {
          cout<< getProductID(productIds, products, 5, "mouse")<<endl;
          cout<< getProductID(productIds, products, 5, "camera")<<endl;
          cout<<getProductID(productIds, products, 5, "laptop")<<endl;
     }
     catch(string str)
     {
          cout<<"Error: "<< str<< " product not found."<< endl;
          cout<<"End of program."<<endl;
 
          return 0;
     }
 
 
     return 0;
 
  }


maybe I should be 'throwing' product instead of target??? or is something wrong with the way I setup the for loop??
Nov 9, 2013 at 2:34pm
the problem is with the if condition, it instantly throws an exception if names[ i ]is not equal to the target, since "mouse" is not first on the list,

the condition :

if( name[ i ] != target )

which can be evaluated to :

if( "computer" != "mouse" )

evaluates to true and throws an exception
Last edited on Nov 9, 2013 at 2:35pm
Nov 9, 2013 at 2:38pm
Hi,

is it possible you should loop through "names[]" ? In line 10 you only compare with "names[1]", which is indeed "flash drive". Maybe change to names[i] != target (?)
Furthermore i think, that you throw your exception a little too early, i.e. when the first comparison doesn't return true .

I think you'll have to count the failures, or just try and try until success and throw when the counter exceeds numProducts ....


Like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int getProductID(int ids[], string names[], int numProducts, string target)
  {      bool success = false;
          for(int i=0; i< numProducts; i++)
          {
                  if (names[i] == target){
                    success = true;
                    break;
                    }
         }
         if(success){
                  return ids[i];
                }
        else{ 
                  throw(target);
                  return ids[i];
                }

 }
  
 


Maybe syntax is bad and so on, but I think the idea should be clear by that.
Last edited on Nov 9, 2013 at 2:39pm
Nov 9, 2013 at 2:41pm
you can do something like :
1
2
3
4
5
6
7
8
9
10
int getProductID(int ids[], string names[], int numProducts, string target)
{
        for(int i=0; i< numProducts; i++)
        {
            if ( names[ i ] == target )
                return ids[ i ];
        }
        
        throw (target);
}
Last edited on Nov 9, 2013 at 2:43pm
Nov 9, 2013 at 3:16pm
so I changed the for loop as shadow fiend suggested and moved the throw code out of the for loop, but it is still returning the 'mouse' as a 'thrown' product.

it seems like that would have made sense though, but it still didn't work.....?
Nov 9, 2013 at 3:17pm
should I move the throw code out of the function/loop and put it in the main somewhere?

****this doesnt seem like it would be right though. it seems like it is supposed to be in function somehow****
Last edited on Nov 9, 2013 at 3:18pm
Nov 9, 2013 at 5:00pm
anyone have an idea?

Thanks......
Nov 10, 2013 at 1:26am
are you sure you have changed also your typo in if() as per your previous code post ? :

if ( names[1] != target ) the index in name[ ] is wrong, it should be i not 1 (one)

and also change != to ==
Topic archived. No new replies allowed.