Table of contents
1.
Introduction
1.1.
Catch Exception
1.1.1.
Example Code: 
1.1.2.
Output
1.2.
Explanation: 
1.2.1.
Example Code: 
1.2.2.
Output
1.3.
Explanation: 
2.
Frequently Asked Questions
2.1.
Why do we need Exception handling in C++?
2.2.
Will it be possible to only include a 'try' block without the 'catch' and 'finally' blocks?
2.3.
What are some examples of unchecked exceptions?
3.
Conclusion
Last Updated: Mar 27, 2024

Catch Block and Type conversion in C++

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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";
   }
}
You can also try this code with Online C++ Compiler
Run Code

Output

This is the default catch block
You can also try this code with Online C++ Compiler
Run Code

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;
   }
}
You can also try this code with Online C++ Compiler
Run Code

Output

This is the default catch block 
You can also try this code with Online C++ Compiler
Run Code

 

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.

Conclusion

In this article, we have extensively discussed Catch block and type conversion in C++. We have discussed the above-mentioned topics with the help of suitable and detailed example codes. We hope this blog has helped you enhance your knowledge regarding the C++ concepts. Some official documentation on big data that can help you improve your understanding is this Pointer in C++ and Local class.

Also Read - C++ Interview Questions

If you would like to learn more, check out our articles on Operator Keyword in C#cloud platform comparison, and 10 AWS best books. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass