Custom exception class : Custom Exception « Exceptions « C++ Tutorial






#include <iostream>
#include <string>

using namespace std;

class Exception {

public:
   Exception(const string& msg) : msg_(msg) {}
  ~Exception( ) {}

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

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

int main( ) {

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








6.5.Custom Exception
6.5.1.Throw your own exception class based on runtime_error
6.5.2.Custom exception class
6.5.3.Throw a custom exception object
6.5.4.Use custom exception in your own Array class