C++ Exceptions

October 27, 2015 Posted by WithU Technologies
There are several problems that occur during program execution or after user input which will crash or terminate the program without any clue for that if we do not apply exceptions handling methods to handle those exceptions.

C++ exceptions is built with three keywords:

try{

throw

catch(){

}

try – try block is used before the block of code where exceptions may generate after execution. It will activate those specific exception part within the program.

throw – throw keyword is used to throw an exception when problems show up along with a value which will be used as error value.

catch – It executes when a particular exception is thrown. You can make a simple output without any error code. But if we still want an error code then a value will be thrown by the throw keyword which will be pass through catch parameter and then executed with message.   
Example:

#include<iostream>
using namespace std;

int main(){

try{   //identifies a block of code that will activate specific exceptions

int n,a=0;

cout<<"Enter your mobile number: ";

cin>>n;   //user input stored in local variable n

while(n>0)  //while loop continues until the n value <  0

{

n=n/10;  //we divide n by 10

a++;    //and after each loop we increase a which is initialized from 0

}

      if(a!=10){      //if the number of digits in mobile number is not equal to 10

            throw 99;    //throw an exception with value of 99 for example

      }

      cout<<"Your mobile number is accepted"<<endl;  //if the number of digits is equal to 10 then this message will be displayed

}

      catch (int x){    //execute to catch the thrown exception and make an output on that with parameter to catch the exception value

            cout<<"Please enter your 10 digits mobile number Error-"<<x<<endl;  //you can also create a simple output without using the error number

      }

return 0;

}

·         A simple program is created above to take input of your mobile number.

·         After that we will calculate the number of digits entered by user.

·         We use while loop to calculate that. Here while loop is looped until the total digits entered by user will be less than 0

·         Inside the while loop we divide the n value by 10 which will exclude each digit from the total number entered by user.

·         And after each exclusion of digit, it will increment the value of variable a which is already initialized with 0.

·         Thus if the user inputted mobile number contains 10 digits then it will output “Your mobile number is accepted”.

·         And if the number of digits is not equal to 10 then it will throw an exception with an example error value of 99.

·         The exception is then caught by the catch keyword along its value and then it makes an output using that value.

 Here the catch handles integer type exceptions. But we can specify that our catch block handles any type of exceptions thrown by a try block.

And for that we use ellipsis between parentheses (…) of catch.

try{
      //Some block of Code

throw;

}

catch(...){  //ellipsis are used to handle any data type

      //Code to handle Exceptions with any data type 
}