C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Exception Handling / Error Handler in C++


Tutorials Cpp

Topic

What is Exception Handling in C++?



Explanation

Exception Handling provides a mechanism to detect and report an exceptional circumstance, so that corrective action can be taken to set right the error occurred. Error Handler in C++ consists of three major keywords namely try, throw and catch. The "try" block is a set of code which generates an exception which is thrown using the "throw" statement. The "catch" block is used to catch the exception and handles it appropriately.

Throw point
Function that causes an exception.
try block
Invokes a function that contains an exception.
catch block
Catches and handles the exception.

Example:

#include <iostream.h>
int main()
 {
   int x,y;
   cout << "Enter values of x::";
   cin >> x;
   cout << "Enter values of y::";
   cin >> y;
   int r=x-y;
   try 
     {
      if ( r!=0)
       {
         cout << "Result of division is x/r:: " << x/r << "\n";
       }
      else
       {
         throw ( r);
       }
     }
   catch( int r)
      {
        cout << "Division by zero exception::
                     value of r is::" << r << "\n";
      }
   cout << "END";
   return 0;
 }

Result:
   Enter values of x::12
   Enter values of y::12
   Division by zero exception::value of r is::0

In the above example, when the value "x" is divided by "0" an exception is thrown. Using the "catch" statement the exception is caught. This is the Error Handler System in C++.








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting