fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define IF_THREW(EXPR, EXC) \
  6. try \
  7. { \
  8.   EXPR; \
  9. } \
  10. catch (EXC)
  11.  
  12. #define OR_THREW(EXC) \
  13. catch (EXC)
  14.  
  15. int computeFoo(int n)
  16. {
  17. if (n == 0)
  18. {
  19. throw "div by 0!";
  20. }
  21. if (n < 0)
  22. {
  23. throw -1;
  24. }
  25.  
  26. return 100 / n;
  27. }
  28.  
  29. int main(void)
  30. {
  31. int foo;
  32.  
  33. IF_THREW(foo = computeFoo(10), const char* excmsg)
  34. {
  35. cout << "exception: " << excmsg << endl;
  36. }
  37.  
  38. cout << foo << endl;
  39.  
  40. IF_THREW(foo = computeFoo(0), const char* excmsg)
  41. {
  42. cout << "exception: " << excmsg << endl;
  43. }
  44.  
  45. IF_THREW(foo = computeFoo(-1), const char* excmsg)
  46. {
  47. cout << "exception: " << excmsg << endl;
  48. }
  49. OR_THREW(int)
  50. {
  51. cout << "exception: negative" << endl;
  52. }
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
10
exception: div by 0!
exception: negative