Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- using namespace std;
- class exc : public exception {
- public:
- const char* what() const noexcept {
- return " Error :: Parameter of class must be positive";
- }
- };
- class IsPositive {
- int a;
- public:
- IsPositive(int a);
- };
- // Parameter of class can be positive only
- IsPositive::IsPositive(int a) {
- a > 0 ? this->a = a : throw exc();
- cout << " Number = " << a << endl;
- }
- void main() {
- vector<int> V;
- // out_of_range
- try {
- V.at(1);
- }
- catch (exception& e) {
- cout << " Out of range :: " << e.what() << endl;
- };
- // lenght_error
- try {
- V.reserve(V.max_size() + 1);
- }
- catch (exception& e) {
- cout << " Length error :: " << e.what() << endl;
- };
- // withOUT error
- try {
- IsPositive a = 5;
- }
- catch (exception& e) {
- cout << e.what() << endl;
- }
- // with error
- try {
- IsPositive a = -5;
- }
- catch (exception& e) {
- cout << e.what() << endl;
- }
- cout << system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement