Guest User

5

a guest
Jun 19th, 2019
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. class exc : public exception {
  7. public:
  8. const char* what() const noexcept {
  9. return " Error :: Parameter of class must be positive";
  10. }
  11. };
  12.  
  13. class IsPositive {
  14. int a;
  15. public:
  16. IsPositive(int a);
  17. };
  18.  
  19. // Parameter of class can be positive only
  20. IsPositive::IsPositive(int a) {
  21. a > 0 ? this->a = a : throw exc();
  22. cout << " Number = " << a << endl;
  23. }
  24.  
  25. void main() {
  26. vector<int> V;
  27.  
  28. // out_of_range
  29. try {
  30. V.at(1);
  31. }
  32. catch (exception& e) {
  33. cout << " Out of range :: " << e.what() << endl;
  34. };
  35.  
  36. // lenght_error
  37. try {
  38. V.reserve(V.max_size() + 1);
  39. }
  40. catch (exception& e) {
  41. cout << " Length error :: " << e.what() << endl;
  42. };
  43.  
  44. // withOUT error
  45. try {
  46. IsPositive a = 5;
  47. }
  48. catch (exception& e) {
  49. cout << e.what() << endl;
  50. }
  51.  
  52. // with error
  53. try {
  54. IsPositive a = -5;
  55. }
  56. catch (exception& e) {
  57. cout << e.what() << endl;
  58. }
  59. cout << system("pause");
  60. }
RAW Paste Data