try, catch, throw Exceptions in C++

try, catch, throw exceptions in C++: In C++ exception handling can be done by using try, catch, throw keywords. In the previous session, we have already learned regarding exception handling occurrence in C++. Let us learn in detail regarding the keywords used in the exception handling.

try, catch, throw exceptions in C++

C++ try exception

The C++ try exception can be used to identify the block of code for which particular exceptions will be activated. While it was followed by one (or) more catch blocks. When a block will raise an exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.

Syntax:

try {
// protected code
} catch( ExceptionName e1 ) {
// catch block
} catch( ExceptionName e2 ) {
// catch block
} catch( ExceptionName eN ) {
// catch block
}

Example:

#include<iostream> 
using namespace std; 
  
int main() 
{ 
   int x = -1; 
  
   //Some code 
   cout<<"Before try \n"; 
   try { 
      cout<<"Inside try \n"; 
      if (x < 0) 
      { 
         throw x; 
         cout<<"After throw (Never executed) \n"; 
      } 
   } 
   catch(int x ) { 
      cout<<"Exception Caught \n"; 
   } 
  
   cout<<"After catch (Will be executed) \n"; 
   return 0; 
}

Output:

Before try
Inside try
Exception Caught
After catch (Will be executed)

c++ try exception

C++ catch exception

The C++ catch exception can be defined by using the catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch. If you want to specify that a catch block should handle any type of exception that is thrown in a try block, you must put an ellipsis, …, between the parentheses enclosing the exception

Syntax:

try {
// protected code
} catch(…) {
// code to handle an exception
}

Example:

#include<iostream>
using namespace std;
double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main () {
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
      z = division(x, y);
      cout << z << endl;
   } catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

Output:

Division by zero condition!

C++ throw exception

The C++ throw exception can be defined that the exceptions can be thrown anywhere within a code block using a throw statement. And the operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.

Example:

#include<iostream> 
using namespace std;  
int main() 
{ 
    try { 
        try  { 
            throw 20; 
        } 
        catch (int n) { 
             cout << "Handle Partially "; 
             throw;   //Re-throwing an exception 
        } 
    } 
    catch (int n) { 
        cout << "Handle remaining "; 
    } 
    return 0; 
}

Output:

Handle Partially Handle remaining