C++ Exception Handling

C++ are providing the exception handling technique. There is not exist in C programming. exception are arise when execute source code. that means this is run time error or problem. Some exception are produce wrong result and some are terminate the program. exception is occurred when not provide valid input to system and some cases logical error having responsible. This is produce abnormal conditions and unknown errors. exception handling is powerful mechanism to deal with runtime error. help of this we can overcome system failure and runtime error situations.

exception handling normally are have two types synchronous and asynchronous.

synchronous exception: This type of exception are occurs when we are try to access undefined array element then they are produce "out of range index". and another example when function calling are infinite then compiler are produce "over flow".

asynchronous exception : Errors such as keyboard interrupts, disk failure,hardware malfunctions those are change the execution flow of program.

Try throw catch block

try : This block are useful to check exceptions. we can check both logical and valid input value. There are normally used in define prediction and detect error.

throw : If try block are detected an exception. then throw are reliable to relative error.

catch :throw are provide uncertain event. and catch statement are fetch this information. In this block are given user information about error. view syntax of exception.

try{
    //statements
    //throw exception
  }catch(type arguments){
    //handle Exception
  }

We are declare multiple exception there are use following syntax.

//Multiple Exception handling     
try{
  //statement
  //check and throw exception;
}catch(type argument){
  //handle exception
}catch(type1 argument){
  //handle exception
}catch(type2 argument){
  //handle exception
}

Note that we can declare multiple (catch and throw statements) of try block. explain all concept in exception handling in this post. first view the execution flow of (try and catch) block.

/*
Execution flow of exception
*/
#include <iostream> 
using namespace std; 
  
int main() { 
  cout<<"Start Main Function.!"<<endl;

  try{
    cout<<"Start try block"<<endl;
    //suppose exception is Occurred
    throw "Exception Are Occurred";
    //statements
    cout<<"End try Block"<<endl;
  }catch (const char *exception){
    //catch and display exception
    cout<<exception<<endl;
  }  
  cout<<"End Main Function.!"<<endl;
    return 0; 
}
Output
Start Main Function.!
Start try block
Exception Are Occurred
End Main Function.!

In this program note that when throw an exception. then compiler are change execution flow of program. and not executing another try block statements. and executed the catch block statements and display exception. look at another example.

/*
Example of exception handling
*/
#include <iostream> 
using namespace std; 

float divide(float x,int y ){

  if(y==0){
    throw "Oops divide by zero try again!";
  }
  return (x/y);
}
  
int main() { 
  float result=0;
  try{

    result=divide(10,5);
    cout<<" Result : "<<result <<endl;
    //this case exception are occurred
    result=divide(10,0);
    cout<<" Result : "<<result<<endl;
    cout<<" Great Job no exception Found"<<endl;
  }catch(const char *info){
    //display exception
    cout<<info;
  }
    return 0; 
}
Output
 Result : 2
Opps divide by zero try agin!

In this example inside try block calling divide function. there scope of (try block) are exist entire divide function. and here check the condition divisor are zero then throw exception. otherwise return proper result of function.

Advantage of Exception Handling

© 2019, regularcodes.com, All rights reserved