fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct TestRuntime {
  7. int operator()()
  8. {
  9. std::string ().replace (100, 1, 1, 'c');
  10. return 1;
  11. }
  12. };
  13.  
  14.  
  15. struct TestExc {
  16. int operator()(int a, int b)
  17. {
  18. throw new std::exception;
  19. return a/b;
  20. }
  21. };
  22.  
  23. int main() {
  24.  
  25. {
  26. // Test handle build-in exception
  27. TestRuntime t1;
  28. try {
  29. int d = t1();
  30. } catch (std::exception &e) {
  31. cout << "t1: std exception: " << e.what() << endl;
  32. } catch (...) {
  33. cout << "t1: unknown exception" << endl;
  34. }
  35. }
  36.  
  37. {
  38. // Test handle "new" exception
  39. TestExc t2;
  40. try {
  41. int d = t2(2,3);
  42. } catch (std::exception &e) {
  43. cout << "t2: std exception: " << e.what() << endl;
  44. } catch (...) {
  45. cout << "t2: unknown exception" << endl;
  46. }
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.02s 2812KB
stdin
Standard input is empty
stdout
t1: std exception: basic_string::replace
t2: unknown exception