Exception Handling in C++


Exception handling in C++ is very easy to do. This is a very brief tutorial on how to handle errors that may arise from code that may give errors.

Keywords

  • try: try is used for identifying a block of code that will have exceptions, its followed by 1 or more catch blocks. In a sense, anything inside the try block is protected code.
  • catch: this keyword catches the exception thrown when the exception is activated within the try block, within the catch block, you also have code that handles the error and
  • throw: you use the throw keyword to throw an exception for code that produces an error or problem

Sample in C++

#include "iostream"
#include "math.h"
#include "windows.h"
#include "exception"
using namespace std;

struct PowerException:public exception//our custom exception example
{
    virtual const char* errorMsg() const throw()
    {
        return "error! no negative values!";
    }
};
int factorial(int n)
{
    if(n==0)
    throw "The factorial of 0 is 0!";
    if(n==1)
        return 1;
    return n*factorial(n-1);
}
int add(int a, int b)
{
    if(a < 0 || b < 0)
    {
        throw new exception();
    }
    else
        return a+b;
}
int powerOf(int x, int y)
{
    if(x < 0 || y < 0)
    {
        throw PowerException();
    }
    if(y==1)
    {
        return x;
    }
    int answer = x * powerOf(x,y-1);
    return answer;
}
int main()
{
    try
    {
        cout<<factorial(0)<<endl;
    }
    catch(const char* message)
    {
        cerr<<message<<endl;
    }

    try
    {
       cout<<add(2,-2)<<endl;
    }
    catch(exception* ex)
    {
        cout<<"no negatives allowed!"<<endl;
    }
    catch(exception* ex)
    {
        //other errors
    }

    try
    {
        cout<<powerOf(3,-3)<<endl;
    }
    catch(PowerException &px)
    {
        cout<<px.errorMsg();
    }

}

In addition to making your own custom exceptions, C++ also has defined exceptions defined in <exception>

Descriptions of the exceptions

Exception Description
std::exception An exception and parent class of all the standard C++ exceptions.
std::bad_alloc This can be thrown by new.
std::bad_cast This can be thrown by dynamic_cast.
std::bad_exception This is useful device to handle unexpected exceptions in a C++ program
std::bad_typeid This can be thrown by typeid.
std::logic_error An exception that theoretically can be detected by reading the code.
std::domain_error This is an exception thrown when a mathematically invalid domain is used
std::invalid_argument This is thrown due to invalid arguments.
std::length_error This is thrown when a too big std::string is created
std::out_of_range This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
std::runtime_error An exception that theoretically can not be detected by reading the code.
std::overflow_error This is thrown if a mathematical overflow occurs.
std::range_error This is occurred when you try to store a value which is out of range.
std::underflow_error This is thrown if a mathematical underflow occurs.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s