Creating an Exception Class - C++ Statement

C++ examples for Statement:try catch

Description

Creating an Exception Class

Demo Code

#include <iostream>
#include <string>

using namespace std;

class Exception {

public:/*  w  ww .  jav a2  s  .  c o m*/
   Exception(const string& msg) : msg_(msg) {}
  ~Exception() {}

   string getMessage() const {return(msg_);}
private:
   string msg_;
};

void f() {
   throw(Exception("Mr. Test"));
}

int main() {

   try {
      f();
   }
   catch(Exception& e) {
      cout << "You threw an exception: " << e.getMessage() << endl;
   }
}

Result


Related Tutorials