Broad Network


Classes and Exceptions in C++

Exception Handling in C++ - Part 5

Forward: In this part of the series, we look at classes and exceptions in C++.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 5 of my series, Exception in C++. In this part of the series, we look at classes and exceptions in C++. Our emphasis will be on constructor functions.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Example
In this example, the class has a constructor function. The constructor function has a try block and two catch blocks. The try block of the constructor function checks if the arguments for the constructor function are within range. After that check, initialization takes place in the try block. Read and try the code that follows:

#include <iostream>
using namespace std;

class theClass
    {
        int first;
        double second;
        public:
            theClass (int aa,double bb)
                {
                    try
                     {
                            if ((aa<0)||(aa>10))
                                throw aa;
                            if ((bb<50)||(bb>70))
                                throw bb;
        
                            //do actual initialization
                            first = aa;
                            second = bb;
                     }
                    catch (int eInt)
                        {
                            cout << "Integer is not in range!";
                        }
                    catch (double eDbl)
                        {
                            cout << "Double is not in range!";
                        }
                }
};

int main()
    {
        theClass theObj(-5, 66.6);

        return 0;
    }

Note: the constructor function is called during instantiation.

Throwing Instantiated Objects
Just as you throw objects of fundamental types, you can throw instantiated objects from classes. The following code illustrates this:

#include <iostream>
using namespace std;

class theClass
    {
        public:
            int first;
            double second;
            theClass (int aa,double bb)
                {
                    first = aa;
                    second = bb;
                }
};

int main()
    {
        try
            {
                theClass theObj(8, 7.7);
                //some checking code
                throw theObj;
            }
        catch (theClass obj)
            {
                cout << "This is illustration, where int of object is " << obj.first;
            }

        return 0;
    }

In the try block, the object is instantiated and it is thrown for some reason. In throwing the instantiated object, you do not need the parentheses with arguments for initialization. The parameter type of the catch block is the name of the class. The name of the class serves as object type here.

Conclusion
We have seen many features in C++ Exception in this series. With these features you can do many things. However, to be an expert in exceptions in C++, you will need to learn the predefined exception classes in the standard library that comes with C++ installations. I will address those soon.

We have come to the end of this series. I hope you appreciated it.

Chrys

Comments

Become the Writer's Fan
Send the Writer a Message