Introduction
C++ Exceptions are runtime abnormalities or unusual circumstances encountered by a program code during execution. These exceptions allow the software to respond to unusual conditions (such as runtime failures) by passing control to special functions known as handlers. Exceptions will enable us to move control from one portion of a program code to another. Exception handling in C++ is based on three keywords: try, catch, and throw.
Here, in this article, we will be discussing the catch block and the type conversion. Also see, Literals in C.Fibonacci Series in C++
Catch Exception
When an error occurs in the try block, the catch statement allows us to define a block of code that will be executed.
Any exception is caught in the catch block that follows the try block. The exception declaration, which occurs in parentheses after the keyword 'catch', determines what exception we wish to catch.
Before moving forwards, let us predict the output of the given C++ code:
Example Code:
#include <iostream>
using namespace std;
int main() {
try{
throw 'a';
}
catch(int a) {
cout << "This is the integer value" << a;
}
catch(...) {
cout << "This is the default catch block";
}
}
Output
This is the default catch block
Explanation:
So, what's the reason for this? Why is this type of output being generated? The letter' a' is thrown, as we can see, but the first catch block is for int. If we believe that the ASCII of 'a' is an integer, we shall enter the first block, but such conversions are not valid for the catch blocks.
Now, let us look at another example.
The conversion function Object() is not invoked for the thrown objects in this example.
Example Code:
#include <iostream>
using namespace std;
class TestExcept1 {};
class TestExcept2 {
public:
TestExcept2 (const TestExcept1 &e ){
// The Conversion constructor is defined here
cout << "From the Conversion constructor";
}
};
main() {
try{
TestExcept1 exp1;
throw exp1;
} catch(TestExcept2 e2) {
cout << "Catch block - TestExcept2 " << endl;
} catch(...) {
cout << "This is the default catch block " << endl;
}
}
Output
This is the default catch block
Try and compile with online c++ compiler.
Explanation:
The derived type objects are not transformed to the base type objects, and they are thrown as an exception. This is known as the type conversion in exceptional handling.
Check out this article - Balanced Parentheses
Frequently Asked Questions
Why do we need Exception handling in C++?
In C++, exception handling is the process of dealing with runtime failures. We handle exceptions so that the application's usual flow may be maintained even when runtime issues occur. An exception in C++ is an event or object thrown during runtime. Exceptions are all derived from the std::exception class.
Will it be possible to only include a 'try' block without the 'catch' and 'finally' blocks?
Including a 'try' block without the 'catch' and 'finally' blocks would result in a compilation error. The 'try' block must be followed by either a 'catch' block or a 'finally' block. To ensure that exception handling flow is not disturbed, either a 'catch' or a 'finally' block is required.
What are some examples of unchecked exceptions?
NullPointerException, ArrayIndexOutOfBoundsException, and NumberFormatException are some of the examples of unchecked exceptions.