Example : A simple program or Exception Handling Examples in C++ to show Exception Handling using the throw keyword.
#include <iostream>
using namespace std;

int main() 
{
    try 
	{
           int a = 10;
           int b = 0;

           if (b == 0) 
	    {
               throw "Division by zero error";
            }

           int c = a/b;
           cout << c << endl;
        } 
   catch(const char* msg) 
      {
        cerr << "Error produced: " << msg << endl;
      }

    return 0;
}

Output:
Error produced: Division by zero error

-----------  OR  -----------

#include <iostream>
using namespace std;
 
int main()
{
    try  
     {
       //throw 'x';
       throw "msg";
     }
    catch (int m)  
     {
        cout << "Exception Caught ";
     }
    return 0;
}

Output: terminate called after throwing an instance of 'char'

-----------  OR  -----------

#include <iostream>
using namespace std;
 
int main()
{
   int x ;
 	cout<<"Enter a value: \n";
 	cin>>x;   
   
   try 
   {      
      if (x == 0)
      	{
         throw x;
         cout << "This statement is Never executed when throw exception arise \n";
      	}
      	cout<<"The accepted value is = "<<x;
   }
   catch (int x ) 
   {
      cout << "Exception Caught \n";
   }   
   return 0;
}

Output:

Enter a value:
0
Exception Caught

-----------  OR  -----------

#include <iostream>
using namespace std;

double division(int m, int n) 
{
   if( n == 0 ) 
	 {
      throw "Exception arise due to division by zero is not occur in computer";
   }
   return (m/n);
}

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

   return 0;
}

-----------  OR  -----------

#include <iostream>
using namespace std;

double division(int m, int n) 
{
   if( n == 0 ) 
   {
      throw "Exception arise due to division by zero is not occur in computer";
   }
   return (m/n);
}

int main () 
{
   int x,y; 
   double z;
 
   try 
    {
      cout<<"Enter two values = ";
      cin>>x>>y;
      
      z = division(x, y);
      cout <<endl<<"Output = "<<z ;
    } 
  catch (const char* msg) 
   {
     cerr << msg << endl;
   }

   return 0;
}
Example : A simple program in C++ to show how to handle re-thrown Exceptions again.
#include <iostream>
using namespace std;
 
int main()
{
    try 
     {
        try 
	 {
            throw 100;
         }
        catch (int x) 
	 {
            cout << "Catch first thrown Exception";
            throw;     // Re-throwing an exception again
         }
    }
    catch (int y) 
      {
        cout << "\nCatch the final and re-thrown Exception";
      }
    return 0;
}

Output:
Catch first thrown Exception
Catch the final and re-thrown Exception
Example : A simple program in C++ to show Exception Handling in Array.
#include <iostream>
using namespace std;

int main() 
{
    try 
     {
        int arr[5] = {10, 20, 30,40,50};
        int i = 6;
        if (i < 0 || i >= 4) 
	{
            throw ("Index out of bounds error");
        }
        
        int x = arr[i];
        cout <<"The given index value is = "<< x << endl;
     } 
   catch(const char* e) 
    {
        cerr << "Error produced: " << e << endl;
    }

    return 0;
}

Output:
Error produced: Index out of bounds error
Example : A simple program in C++ to show Multiple Catch Exception Handling.
#include<iostream>
using namespace std;
 
int main()
{
    int x;
	cout<<"Enter a value= ";
	cin>>x;
    try  
    {
       throw x;
    }
    catch (char *msg)  
    {
        cout << "Specific Exception Caught " << msg;
    }
    catch (...)  
    {
        cout << "Default Exception arised\n";
    }
    return 0;
}

Output:
Enter a value = 10
Default Exception arised
         OR
Enter a value = abc
Default Exception arised

----------  OR  -----------

#include <iostream>
using namespace std;

int main() 
{
    
    double m, n; 
	int x[5] = {10, 20, 30, 40,50};
    int i;
    
    cout << "Enter array index value: ";
    cin >> i;
    
    try 
			{        
        // throw exception if array out of bounds
        if (i >= 4)
        {
           throw "Array out of bounds exception occurs";
	}  
        
        cout << "Enter first value: ";
        cin >> m;
    
        cout << "Enter Second value: ";
        cin >> n;
        
        if (n == 0)
        {
           throw 0;
	}				
        
        x[i] = m / n;
        cout << x[i] << endl;
    }
    
    catch (const char* msg) 
    {
        cout << msg << endl;
    }
    
    catch (int num) 
    {
      cout << "Exception arise : Cannot divide by zero or " << num << endl;
    }

    // catch any other exception
    catch (...) 
    {
        cout << "A Unexpected exception arise" << endl;
    }
    
    return 0;    
}

Output:
Error produced: Division by zero exception

-----------  OR  -----------

#include <iostream>
using namespace std;

int main() 
{
    
    double m, n; 
	int x[5] = {10, 20, 30, 40,50};
    int i;
    
    cout << "Enter array index value: ";
    cin >> i;
    
    try 
	{        
        // throw exception if array out of bounds
        if (i >= 4)
        {
           throw "Array out of bounds exception occurs";
	}  
        
        cout << "Enter first value: ";
        cin >> m;
    
        cout << "Enter Second value: ";
        cin >> n;
        
        if (n == 0)
        {
           throw 0;
	}				
        
        x[i] = m / n;
        cout << x[i] << endl;
    }    

    // catch any exception
    catch (...) 
    {
        cout << "A Unexpected exception arise" << endl;
    }    
    return 0;    
}

Loading

Categories: C++

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.