Sunday, May 11, 2014

Throw and Catch Custom Exception Object in C++

Exceptions are run-time errors that occur during program execution. C++ Standard Library comes with a base class "exception" which is mainly dealing with exceptions. In some circumstances it is required to create-design your own custom exception classes in C++ as in other object oriented programming languages.
what() function of exception class of C++ Standard Library used to provide detailed information about the problem to the client program code in the case of an exception.

Following sample project created by qt creator and contains following files:
1- CustomException.pro
2- DequeWrapper.h
3- DequeWrapper.cpp
4- EmptyContainerException.h
5- EmptyContainerException.cpp
6- main.cpp

CustomException.pro file contains project configuration.

TARGET = CustomException
TEMPLATE = app
SOURCES += main.cpp \
           DequeWrapper.cpp \
           EmptyContainerException.cpp

HEADERS += \
           DequeWrapper.h \
           EmptyContainerException.h

main.cpp file contains main method and the usage of the EmptyContainerException exception instance.
#include "DequeWrapper.h"
#include <iostream>

using namespace std;
using namespace CustomContainers;

int main()
{
    try
    {
        DequeWrapper mDequeWrapper;
        mDequeWrapper.popBack();
    }
    catch(const EmptyContainerException& ex)
    {
        cerr << ex.what() << endl;
    }

    return 0;
}

DequeWrapper.h is the header file for the deque wrapper class declaration.
#ifndef DEQUEWRAPPER_H
#define DEQUEWRAPPER_H

#include <deque>
#include "EmptyContainerException.h"

namespace CustomContainers
{

class DequeWrapper
{
public:
    bool empty() const;
    int popBack();

protected:
    std::deque mDeque;
};

}
#endif // DEQUEWRAPPER_H

DequeWrapper.cpp is the implementation file for the deque wrapper header file.

#include "DequeWrapper.h"

namespace CustomContainers
{

bool DequeWrapper::empty() const
{
    return mDeque.empty();
}

int DequeWrapper::popBack()
{

    if(mDeque.empty())
        throw EmptyContainerException();

    int mItem(mDeque.back());
    mDeque.pop_back();
    return mItem;
}

}
EmptyContainerException.h file is the header file for the custom exception class declaration.
#ifndef EMPTYCONTAINEREXCEPTION_H
#define EMPTYCONTAINEREXCEPTION_H

#include <exception>

namespace CustomContainers
{

class EmptyContainerException : public std::exception
{
    public:
        virtual const char* what() const throw();
};

}

#endif // EMPTYCONTAINEREXCEPTION_H

EmptyContainerException.cpp file is the implementation file for the custom exception class header file.

#include "EmptyContainerException.h"

namespace CustomContainers
{

const char* EmptyContainerException::what() const throw()
{
    return "container is empty!";
}

}

std::exception what() method is reimplemented in the derived class EmptyContainerException to provide the description of the exception. When you run the above sample program main method following console output "container is empty" exception message is generated :


No comments:

Post a Comment