Exception handling in C++ !!!

Hello everyone!!! I am here with my newest post related to exceptions. So here we go.....
What is exception handling?
       We know compile time errors and run time errors. When we run our program, at that time unavoidable errors (run time) occurs. To prevent this errors. c++ provides exception function which is used to handle this circumstances. This process is called exception handling.

       Main aim of exception handling mechanism is to detect and report the exceptional situation. So that appropriate action can be taken. It performs following tasks!!!

1) Find problems.
2) Report the errors. (throw the exception)
3) Obtain the information about the errors. (catch the exception)
4) Perform some appropriate actions. (handling an exception) 

Exception handling mechanism process contains three keywords:
1 - try
2 - catch 
3 - throw

try:- It represents the block of statements in which there are chances of occurring some exceptional conditions.
  
catch:- Catch is used to handle the thrown exception.

throw:- When exception detects, it is throwing using the throw keyword.

     When we use try keyword in the program, there are minimum one catch required. We can use multiple catches under one try keyword.
Syntax is :-
try 
{

//throw keyword;
//body of try

}
catch(argument)
{

  //body of catch

}

   The try contains the portion of the program that is to be examined for error detection process. If error occurs in the program then it is thrown using throw.
Using catch, the exception is caught and processed. Catch keyword is immediately followed by the try keyword.
Lets take an example of this concept.
#include<iostream.h>
void main()
{
   try
   {
       throw 200;

   }
catch (int x)
{
cout << " An exception " <<x<<endl;
}
}
   In above example, try block contains the portion of the code for exception handling and thrown by a throw statement. It accepts one parameter 200 which is passed to exception handler as argument.
Important point is that we can use multiple catches under one try block.  here is the syntax.
void fun
{
 ...
  try
  {
   ....
  } 
  catch(type1 arg)
  {
   ....
  }
  catch (type2 arg)
  {
   ....
  }
  catch (type n arg)
  {
    ...
  }
.....
}

here is an example:-

#include<iostream.h>
void fun(int n)
{

   try
   {
    if(num)
        throw n;
     else
         throw "value is one";
    }
catch(int i)
{
cout<< "exception for numbers are handled: "<<i<<"\n";
catch(char *str)
{
 cout<< "" exception for strings are handles: ";
cout<str<<"\n";
 }
}
 void main()
{
cout<<"continue" <<endl;
function(0);
function(1);
function(2);
function(3);
cout<<"stop"<<endl;
}

 We can see that in the program,we use try block is one and catch block is more than one.

So this is all about exception handler!!!!

My next post will be coming soon!!!

Until then bye bye!!!

follow us here for different updates

facebook: CLICK HERE!!!!!!!!!!
 
Twitter: @NewAgeInformers
 
Instagram: @new_age_informers_

Comments

Post a Comment