1#include <iostream> 2#include <stdexcept> 3#include <string> 4 5#include <iostream> 6 7void g(int n) 8{ 9 if (n == 17) { 10 std::cerr << "Throwing an exception" << std::endl; 11 throw std::invalid_argument("17 is a bad number"); 12 } 13 std::cerr << "Not throwing an exception" << std::endl; 14} 15 16int do_test_2(int n) 17{ 18 std::cerr << "Entering do_test_2\n"; 19 try { 20 g(n); 21 } 22 catch(const std::invalid_argument& e) { 23 std::cerr << "Caught an exception: " << e.what() << std::endl; 24 return 1; 25 } 26 catch(...) { 27 std::cerr << "Caught an unexpected exception" << std::endl; 28 return 2; 29 } 30 31 std::cerr << "Normal exit from do_test_2\n"; 32 return 0; 33} 34 35extern "C" int do_test(int n); 36 37int main() 38{ 39 std::cerr << "About to call do_test" << std::endl; 40 int result = do_test(16); 41 std::cerr << "Return value is " << result << std::endl; 42 43 std::cerr << "About to call do_test_2" << std::endl; 44 int result2 = do_test_2(17); 45 std::cerr << "Return value is " << result2 << std::endl; 46} 47