fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. string findItem(string *array, int size, string target)
  8. {
  9. int index = 0;
  10. bool found = false ;
  11.  
  12. while (!found && (index < size))
  13. {
  14. if (target == array[index])
  15. found = true ;
  16.  
  17. else
  18. index++;
  19. }
  20.  
  21.  
  22. if (!found)
  23. throw runtime_error("Target not found in a array!");
  24.  
  25. return array[index];
  26. }
  27.  
  28.  
  29. int main(){
  30.  
  31. string stuff[5] = {"Keys", "Food", "Glasses", "Cheese", "Skeleton"};
  32.  
  33. string a_thing;
  34.  
  35. try
  36. {
  37. a_thing = findItem(stuff, 5, "Pants");
  38.  
  39. }
  40.  
  41. catch(runtime_error& runErr)
  42. {
  43. //runErr.what() still prints nothing.
  44. cout << runErr.what() << endl;
  45. cout << "runtime_error" << endl;
  46.  
  47. a_thing = "Nothing";
  48.  
  49.  
  50. }
  51.  
  52. cout << a_thing << endl;
  53.  
  54. return 0;
  55.  
  56. }
  57.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Target not found in a array!
runtime_error
Nothing