Programming Code Examples

How to Throw Exception of the Class Type in C++2 min read

Instead of throwing exceptions of pre-defined types like int, float, char, etc., we can create classes and throw those class types as exceptions. Empty classes are particularly useful in exception handling. Following program demonstrates throwing class types as exceptions:

Input and output for the above program is as follows:

In the above program ZeroError is an empty class created for handling exception.

Exception Handling and Inheritance

In inheritance, while throwing exceptions of derived classes, care should be taken that catch blocks with base type should be written after the catch block with derived type. Otherwise, the catch block with base type catches the exceptions of derived class types too. Consider the following example:

Output for the above program is as follows:

You can see that in the above program even though the exception thrown is of the type Derived it is caught by the catch block of the type Base. To avoid that we have to write the catch block of Basetype at last in the sequence as follows:

Output of the above program is as follows:

Exceptions in Constructors and Destructors

It is possible that exceptions might raise in a constructor or destructors. If an exception is raised in a constructor, memory might be allocated to some data members and might not be allocated for others. This might lead to memory leakage problem as the program stops and the memory for data members stays alive in the RAM.

Similarly, when an exception is raised in a destructor, memory might not be deallocated which may again lead to memory leakage problem. So, it is better to provide exception handling within the constructor and destructor to avoid such problems. Following program demonstrates handling exceptions in a constructor and destructor

Take your time to comment on this article.

Add comment