Cpp Tutorial: Exception Handling in C++

An exception is an error condition that occurs during run time.

Generally without exception handling, the program will crash when there is an exception.

We place the code where the exception might occur and we handle that exception without crashing the program.

C++ provides 3 types of exception handling keywords:

try, catch, throw.

try: We use this keyword to place the piece of code where the exception might occur.

catch: This block of code is executed when an exception occurs.

throw: This keyword is used to throw an error when an exception is created. “catch” block will catch the exception thrown.

Syntax for exception handling:

try
{
	//statements that exception might occur

	throw exception;
}

catch(Exception e)
{
	//statements to execute when exception is thrown
}

Now let us learn exception handling with various examples:

Example 1: Simple exception handling.

#include <iostream>  
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  

int main() 
{ 
   int num = -1; 
  
   cout << "Before try \n"; 
   try 
   { 
      cout << "In try \n"; 
      if (num < 0) 
      { 
         throw num; 
         cout << "After throw, it will not be executed \n"; 
      } 
   } 
   catch (int num ) 
   { 
      cout << "Inside \"catch\". Exception Caught \n"; 
   } 
  
   cout << "After catch this Will be executed \n"; 
   return 0; 
} 

Output:

Before try 
In try 
Inside "catch". Exception Caught 
After catch this Will be executed

Example 2: To catch all kind of exception use “catch(…)”

#include <iostream>  
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  

int main() 
{ 
   int num = -1; 
  
   cout << "Before try \n"; 
   try 
   { 
      cout << "In try \n"; 
      if (num < 0) 
      { 
         throw num; 
         cout << "After throw, it will not be executed \n"; 
      } 
   } 
   catch (... ) 
   { 
      cout << "Inside generic \"catch\". \n"; 
   } 
  
   cout << "After catch this Will be executed \n"; 
   return 0; 
} 

Output:

Before try 
In try 
Inside generic "catch". 
After catch this Will be executed

Example 3: Throwing different types of exceptions:

#include <iostream>  
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  

int main() 
{ 
   int num = -1; 

   try 
   { 
      if (num < 0) 
      { 
         throw num; 
      } 
      else
      {
          throw "error";
      }
   } 
   catch (int x ) 
   { 
      cout << "Inside integer \"catch\" block. \n"; 
   } 
 
   catch (char x ) 
   { 
      cout << "Inside char \"catch\" block. \n"; 
   }  

   return 0; 
} 

Output:

Inside integer "catch" block.

Example 4: If the exception thrown is not caught by any “catch” statement, then it will result in runtime error

#include <iostream>  
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  

int main() 
{ 
   int num = -1; 

   try 
   { 
      if (num < 0) 
      { 
         throw num; 
      } 

   } 

   catch (char x ) 
   { 
      cout << "Inside char \"catch\" block. \n"; 
   }  

   return 0; 
} 

Output:

terminate called after throwing an instance of 'int'

Example 5: When an exception is thrown, all the objects inside “try” block will be destroyed.

#include <iostream>  
// for more tutorial in C++ visit www.prodevelopertutorial.com
using namespace std;  

class MyClass
{
    public:
    
    MyClass()
    {
        cout<< "MyClass Constructor:"<<endl;
    }
    
    ~MyClass()
    {
        cout<< "MyClass destructor:"<<endl;
    }
};
int main() 
{ 

   try 
   { 
        MyClass obj;
        
        throw 1; 
   } 

   catch (int x ) 
   { 
      cout << "Inside \"catch\" block. \n"; 
   }  

   return 0; 
} 

Output:

MyClass Constructor:
MyClass destructor:
Inside "catch" block.

 

 

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *