cpp:exception_inheritance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:exception_inheritance [2019/05/06 08:11]
gthanos
cpp:exception_inheritance [2020/05/04 12:06]
gthanos
Line 6: Line 6:
 using namespace std; using namespace std;
  
-class BaseException: public std::exception {+class BaseException {
 protected: protected:
   int a;   int a;
Line 12: Line 12:
 public: public:
   BaseException(int a) { this->a = a; }   BaseException(int a) { this->a = a; }
-  const char* what() const throw() {+  virtual char* message() {
     sprintf(s, "BaseException, a: %d\n", a);     sprintf(s, "BaseException, a: %d\n", a);
     return s;     return s;
Line 27: Line 27:
 public: public:
   DerivedException(int a, int b): BaseException(a) { this->b = b; }   DerivedException(int a, int b): BaseException(a) { this->b = b; }
-  const char* what() const throw() {+  char* message() {
     sprintf(s, "DerivedException, a: %d, b: %d\n", a, b);     sprintf(s, "DerivedException, a: %d, b: %d\n", a, b);
     return s;     return s;
Line 55: Line 55:
     }     }
   } catch(BaseException ex) {   } catch(BaseException ex) {
-    cout << ex.what();+    cout << ex.message();
   } catch(DerivedException ex) {   } catch(DerivedException ex) {
-    cout << ex.what();+    cout << ex.message();
   }   }
   return 0;   return 0;
Line 112: Line 112:
     }     }
   } catch(BaseException &ex) {   } catch(BaseException &ex) {
-    cout << ex.what();+    cout << ex.message();
   } catch(DerivedException &ex) {   } catch(DerivedException &ex) {
-    cout << ex.what();+    cout << ex.message();
   }   }
   return 0;   return 0;
cpp/exception_inheritance.txt · Last modified: 2023/05/15 14:08 by gthanos