C++

C++ Exception Handling

Exception is any abnormal behavior or run time error. Exception are offbeat situation in our program where our program should be ready to handle it with appropriate response.

Where Exception occurs

Whenever we run any software, we expect that our software works properly in any situation. But in some instances, some events occur when the normal flow of execution of our software is stopped. So, when we say that our software must be robust, it means software works properly at any situation.

Exception Handling

C++ exception handling is used to handle the error condition in a program systematically by taking the necessary action. Using exception handling, we can easily manage and respond to run time error.

Try, Catch, Throw

  1. If any exceptional situation occurs in our program, we contain it in a block called try block.
  2. If any exception occurs within the try block, it has to be thrown using the keyword throw.
  3. That exception is caught by the catch block. In the catch block necessary action are taken to handle the exception.

Syntax

                 try {
               
               }
                 Catch ( type1 arg ) {
               }
             Catch ( type2 arg ) {
               }
            ----------
        Catch ( typeN arg ) {
               }

Explanation
Firstly, we use to write the keyword try. After ending the try block, we have to write catch block immediately. We can write multiple catch blocks in sequence. But where try block end, all the catch blocks have to be written immediately. If we do not write catch block immediate after the try block, error may occur. We use throw keyword inside the try block.

Programming Example 1

# include<iostream>
Using namespace std ;
  int  main()
     {
    cout<< “ Exception Handling Program :;
    try {
        throw 30 ;
        cout<< “ \n Try ” ;
         }
    catch( int e ) {
        cout<< “ \n Exception no :<< e ;
    }
    cout<< “ \n Testing The Last Line ” ;
}

Output

Programming Explanation
In this programming example, we are trying to show how exception occur and how to handle it. Inside the main function after writing an output statement we use try block. Throw keyword is used inside the try block. After throw keyword we use a number 30. It can be a string, or a double or a float— any constant we can use. Then we write a cout statement. But this line cannot be executed after the execution of throw. After finishing the try block, we write catch block immediately. Inside the catch block we write the line ( int e). With the help of throw keyword which value we throw, that value will come inside the e variable. After ending the catch block, we write an output statement “\n testing The Last Line”. Now, the question is when this line will be executed after the output statement inside the catch block?

Rules of try, catch, and throw block

  1. If we write try block, we have to write catch block.
  2. If we write only catch block and not write any try block and throw, error will occur.
  3. If we write throw and catch block and not write any try block, error will occur.
  4. If we write only throw and not write any try and catch block, no error will occur. It means we can write throw without write the try block.
  5. If we write multiple catch block with different type of arguments in sequence, which throw value will match the type of catch argument, it will accept it. Then no other catch block will be executed. The programming example is given below.

Programming Example 2

# include<iostream>
Using namespace std ;
  int  main()
     {
    cout<< “ Exception Handling Program :;
    try {
        if( i==1)
          throw 1 ;

if( i==2)
          throw 2 ;
if( i==3)
          throw 3 ;
        cout<< “ \n Try ” ;
         }
    catch( double e ) {
        cout<< “ \n Exception no :<< e ;
    }
    catch( int e ) {
        cout<< “ \n Exception no :<< e ;
    }

    cout<< “ \n Testing The Last Line ” ;
}

Output

f. We can write multiple throw statements in a single try block.

Programming Example 3

# include<iostream>
using namespace std ;
  int  main()
     {
          int i=3 ;
    cout<< “ Exception Handling Program :;
   
    try {
        if( i==1)
          throw 1 ;
if( i==2)
          throw 2 ;
if( i==3)
          throw 3 ;    
        cout<< “ \n Try ” ;
         }
   
catch( int e ) {
        cout<< “ \n Exception no :<< e ;
    }

         cout<< “ \n Testing The Last Line ” ;
}

Output

g. We can write throw statement inside a try block without giving any throw value.

Programming Example 4

# include<iostream>
using namespace std ;
  int  main()
     {
    cout<< “ Exception Handling Program :;
    int i=3 ;
    try {  
if( i==3)
          throw ;      
        cout<< “ \n Try ” ;
         }
   
catch( int) {
        cout<< “ \n Exception no :;
    }

    cout<< “ \n Testing The Last Line ” ;
}

Output

Explanation

Here we write throw keyword without giving any value, then the program will be terminated because proper exception handling does not occur here. As a result, catch block will not be executed. In compile time there will be no error. But in run time the program will be terminated with a message “terminate called without an active exception”.

Conclusion

Discussing in detail about the exception handling, we learn here that if any abnormal situation occur in our program, program will have to be ready to manage this abnormal situation.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com