Catch Block and Type Conversion in C++

Introduction

A catch block in C++ is used to handle exceptions that are thrown during the execution of a try block and type conversion in C++ refers to the process of converting a value from one data type to another. On this page we will discuss about the catch block and type conversion in C++.
Catch Block and Type Conversion in C++

Catch Block

The catch block specifies a type of exception that it can handle, and the code inside the catch block is executed when an exception of that type is thrown.

Syntax:

try {
    // code that may throw an exception
} catch (exceptionType e) {
    // code to handle the exception
}

Here, "exceptionType" is the type of exception that the catch block can handle, and "e" is a variable that holds the exception object. The exception object can be used to access information about the exception, such as the error message or stack trace.

You can also use catch block without any exception type which will catch any exception thrown in try block.

try {
    // code that may throw an exception
} catch (...) {
    // code to handle the exception
}

Type conversion in Exceptional handling

Type conversion in exceptional handling in C++ refers to the process of converting an exception object from one type to another. In C++, when an exception is thrown, the catch block that will handle the exception is determined by matching the type of the exception object to the type specified in the catch block. If the exception object is of a different type than the catch block, then the catch block will not be executed.

To handle this scenario, C++ allows for type conversion of exception objects so that a catch block can handle an exception object of a different type than what is specified in the catch block. This is done by using a conversion constructor, which is a constructor that takes a single argument of a different type and converts it to the type of the class.

Example 1:

Run
#include <iostream>
using namespace std;
 
int main()
{
    try
    {
        throw 'p';
    }
    catch(int p)
    {
        cout << "Integer value is caught :" << p;
    }
    catch(...)
    {
        cout << "Default catch block";
    }
}

Output:

Default catch block
The above program throws an exception of character type, ‘p’ and there is a catch block to catch an exception of int type. One could assume that the catch block for int should match with the ASCII value of ‘p’, however, catch blocks do not perform such type of conversion.

Example 2:

Run
#include <iostream>
using namespace std;

class MyException1 {};
class MyException2
{
public:
  // Defining the Conversion constructor
  MyException2 (const MyException1 &e)
  {	
    cout << "From the Conversion constructor";
  }
};
int main()
{
  try
  {
    MyException1 exp1;
    throw exp1;
  } 
  catch (MyException2 e2)
  {
    cout << "Caught MyException2 " << endl;
  } 
  catch ( ...)
  {
    cout << "Default catch block " << endl;
  }
}

Output:

Default catch block 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription