C++ User Defined Exceptions
Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined class types. Within the try block, we may write to throw an exception of type demo_exception class.
throw demo_exception();
Example:
#include <iostream>
#include <bits/stdc++.h>
#include <stdlib>
using namespace std;
class demo_exception {
};
int main()
{ try {
throw demo_exception();
}
catch (demo_exception d) {
cout << "Caught exception of demo class \n";
}
return 0;
}
OUTPUT:
Caught exception of demo_exception class
…………………………………………………………………
Process executed in 3213.33 seconds
Pres any key to continue.
Explanation:
We declared an empty class in the program. We toss an object of type demo class in the try block. The try block catches and shows the item.
Let's look at a basic user-defined exception example that uses the std::exception class to specify the exception.
#include <iostream>
#include <exception>
#include <bits/stdc++.h>
#include <stdlib>
using namespace std;
class My_exception : public exception{
public:
const char * what() const throw()
{
return "Attempted to divide by zero!\n";
}
};
int main()
{
try
{
int i, j;
cout << "enter the two numbers : \n";
cin >> i >> j;
if (y == 0)
{
My_exception k;
throw k;
}
else
{
cout << "i / j = " << i/j << endl;
}
}
catch(exception& e)
{
cout << e.what();
}
return 0;
}
OUTPUT:
enter the two numbers :
50
5
x / y = 10
……………………………………
Process executed in 121.2 seconds
Press any key to continue.
Explanation:
What() is a public function given by the exception class in the example above. It's used to find out what caused an exception.
Let us see an example of two-class program to implement exception handling:
#include <iostream>
#include <bits/stdc++.h>
#include <stdlib>
#include <exception>
using namespace std;
class demo_exception1 {
};
class demo_exception2 {
};
int main()
{
for (int i = 1; i <= 2; i++) {
try {
if (i == 1)
throw demo_exception1();
else if (i == 2)
throw demo_exception2();
}
catch (demo_exception1 d1) {
cout << "Caught exception of demo_exception1 class \n";
}
catch (demo_exception2 d2) {
cout << "Caught exception of demo_exception2 class \n";
}
}
}
OUTPUT:
Caught exception of demo_exception1 class
Caught exception of demo_exception2 class
……………………………………………………………………
Process executed in 1112. Seconds
Press any key to continue.
Handling exceptions via inheritance
Exception handling can also be accomplished using inheritance. In the case of inheritance, the first catch block catches the object thrown by the derived class.
Example:
#include <iostream>
#include <bits/stdc++.h>
#include <stdlib>
#include <exception>
using namespace std;
class demo_exception1 {
};
class demo_exception2 : public demo_exception1 {
};
int main()
{
for (int a = 1; a <= 2; a++) {
try {
if (a == 1)
throw demo_exeception1();
else if (a == 2)
throw demo_exception2();
}
catch (demo_exception1 d1) {
cout << "Caught exception of demo_exception1 class \n";
}
catch (demo_exception2 d2) {
cout << "Caught exception of demo_exception2 class \n";
}
}
}
OUTPUT:
Caught exception of demo_exception1 class
Caught exception of demo_exception1 class
…………………………………………………………………..
Process executed in 0.122 seconds
Press any key to continue.
Explanation:
The program is similar to the last one, only demo_exception2 is now a derived class of demo_exception1. It's worth noting that the demo_exception1 catch block is written first. Demo_exception1 is the base class for demo_exception2, therefore any object thrown by demo_exception2 will be dealt with by the first catch block. As a result, the output is as indicated.
Constructor-based exception handling
Constructor-based exception handling is also possible. Although the function Object () { [native code] } cannot return any value, the try and catch block can.
Example:
#include <iostream>
#include <stdlib>
#include <bits/stdc++.h>
using namespace std;
class demo_exception {
int number;
public:
demo_exception(int i)
{
try {
if (i == 0)
// catch block would be called
throw "Zero not allowed ";
number = i;
show();
}
catch (const char* exptn) {
cout << "exception caught \n ";
cout << exptn << endl;
}
}
void show()
{
cout << "Number = " << number << endl;
}
};
int main()
{
// constructor will be called
demo_exception(0);
cout << "Again creating object \n";
demo_exception(1);
}
OUTPUT:
exception caught
Zero not allowed
Again creating object
Number = 1
....................
Process executed in 0.342 seconds
Press any key to continue.
Explantion:
When i equal 0, an exception is raised and the catch block is invoked. When i = 1, no exception is thrown.
Let’s look at another example of user defined exception in C++ and try to visualize it:
#include <iostream>
#include <bits/stdc++.h>
#include <tsdlib>
using namespace std;
int main()
{
int i = -1;
try {
cout << "Inside try \n";
if (i < 0)
{
throw i;
cout << "After throw \n";
}
}
catch (int i ) {
cout << "exception Caught \n";
}
cout << "After catch \n";
return 0;
}
OUTPUT:
Inside try
Exception Caught
After catch
Explanation:
Lines of the try block after the throw statement are not performed when an exception is thrown. The code after the catch block is run when an exception is captured. Catch blocks are usually written at the conclusion of a sentence.