When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. 

When an error occurs, C++ will normally stop and generate an error message.

 The technical term for this is: C++ will throw an exception (throw an error)

The try statement allows you to define a block of code to be tested for errors while it is being executed.

 The throw keyword throws an exception when a problem is detected, which lets us create a custom error.

 The catch statement allows you to define a block of code to be executed, if an error occurs in the try block


Example:

#include<iostream>

using namespace std;

int main()

{

try

{

    int age=15;

    if ( age >= 18 )

    cout << " you can drive " << endl;

    else 

    throw ( age );

}

catch ( int n )

{

    cout << "Age is " << n << " which is  less than 18 ";

}

}


Error handling to see if numbers are equal


#include<iostream>

using namespace std;


int main()

{

    try

    {

        cout << "Enter 2 nos " << endl;

        int n , m;

        cin >> n >> m;

        if(n==m)

        cout << " Nos are equal ";

        else

        throw( n );

    }

    catch( int i )

    {

        cout << i << " is not equal to other number ";

    }

}


There can be multiple catch statements as well as per requirement