fork(1) download
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class above20
  7. {
  8. public:
  9. above20()
  10. {
  11. msg = "Number is above 20.";
  12. }
  13.  
  14. void getmsg()
  15. {
  16. cout<<msg<<endl;
  17. }
  18.  
  19. private:
  20. string msg;
  21. };
  22.  
  23.  
  24. class below10
  25. {
  26. public:
  27. below10()
  28. {
  29. msg = "Number is below 10";
  30. }
  31.  
  32. void getmsg()
  33. {
  34. cout<<msg<<endl;
  35. }
  36.  
  37. private:
  38. string msg;
  39. };
  40.  
  41. int main ()
  42. {
  43. int num;
  44. try
  45. {
  46. cout<<"Enter Number between 10 and 20:"<<endl;
  47. cin >> num;
  48.  
  49. if (num > 20)
  50. {
  51. throw above20 ();
  52. }
  53.  
  54. if (num < 10)
  55. {
  56. throw below10 ();
  57. }
  58. }
  59. catch (above20 obj)
  60. {
  61. obj.getmsg();
  62. }
  63. catch (below10 obj)
  64. {
  65. obj.getmsg();
  66. }
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0s 3436KB
stdin
21
stdout
Enter Number between 10 and 20:
Number is above 20.