Writing Custom Exception Class In C++

Custom Exception Class in c++

Follow the series “C++ By Examples” for all the C++ tutorials.

Exceptions is a way to handle run time errors arise during the execution of program. There are multiple types of exceptions. That includes bad_alloc, bad_cast, bad_typeid, logic_error, domain_error, invalid_argument, out_of_range, runtime_error, range_error, overflow_error, underflow_error and bad_exception. I listed all exceptions here on purpose. Because to find exception types again, you don’t need to wade through c++ source! And for enthusiasts, these exception types are defined in /usr/include/c++/4.4/bits/functexcept.h file. Let’s get back to the topic. I assume, basic usage of exceptions known to you. In this article I’ll show example on how-to write your own exception class which derives from the standard exception class ( Source at /usr/include/c++/4.4/exception). 

Our custom Exception class will derive from the std::exception class and override what() method. There is one demo class of which method f() throws our custom Exception. We put that code in try-catch block to handle the custom exception thrown.

Below is the file 005_use_exception.cpp which houses all the action… Makefile can be used from this article.


/**
* File Name : 005_use_exception.cpp
* Functionality : Overriding the standard exception class to make your
 own Exception class

* Copyright (C) 2012 Divyang Patel

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <exception>

using namespace std;

class Exception : public exception
{
 public:
  Exception(const char* errMessage):errMessage_(errMessage){}
  // overriden what() method from exception class
  const char* what() const throw() { return errMessage_; }

 private:
  const char* errMessage_;
};

class DemoEx
{
 public:
  DemoEx() throw(Exception&);
  void f() throw(Exception&);
};

DemoEx::DemoEx() throw(Exception&)
{
 try
 {
  f();
 }
 catch(Exception& e)
 {
  cout << "Caught Exception in ctor of DemoEx" << endl;
  cout << e.what() << endl;
 }
}

void DemoEx::f() throw(Exception&)
{
 throw Exception("Exception explanation string called from e.what()");
}

int main()
{
 DemoEx dobj;
 return 0;
}

The output is as follows


Caught Exception in ctor of DemoEx
Exception explanation string called from e.what()

Any doubts? Post in comments..

Related Posts

C++ Friend class and function

C++ Friend Function And Friend Class Example

Ideal Way Of Exception Handling In Multi Layer Java Web Application

C++ Static Keyword Example

Static Method And Variables In C++

Array Of Objects Having Virtual Functions In C++

Leave a Reply









Ad

Like Us!