Exceptions Handling in C++





What is Exception ?

It's an abnormal condition that occurs during the execution of program, and due to which the normal flow of program's instructions may disrupt.

Consider the following example:
int main()
{
 cout<< 5/0;
 cout<<"hello";
//do something 
 retutn 0;
}              
Guess the output:your program will stop .

To handle exceptions in C++, C++ provides three keywords: try, catch, and throw.
  • throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

  • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

  • try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block:

// exceptions
#include<iostream>
using namespace std;

int main () 
{
  try
  {
    throw 1;
  }
  catch (int e)
  {
    cout << "An exception occurred."; 
  }
  return 0;
}                
OUTPUT:
An exception occurred.
  • The code under exception handling is enclosed in a try block. In this example this code simply throws an exception:
    throw 1;

  • A throw expression accepts one parameter (in this case the integer value 1), which is passed as an argument to the exception handler.

  • The exception handler is declared with the catch keyword immediately after the closing brace of the try block.

  • The type of this parameter in catch is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught by that handler.

  • You can use multiple handler (catch blocks..) for a single try with different parameter type.

    try {
      // code here
    }
    catch (int param) { cout << "int exception"; }
    catch (char param) { cout << "char exception"; }
    In that case,Only the handler whose argument type matches the type of the exception specified in the throw statement is executed.
  • You can use an ellipsis ( ... ) as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown.

    try {
          throw 1;
      // code here
          throw 'e';
    }
    catch ( ... ) { cout << "int exception"; }
  • It is also possible to nest try-catch blocks within more external try blocks.

    try {
      try {
          // code here
      }
      catch (int n) {
          throw;
      }
    }
    catch (...) {
      cout << "Exception occurred";
    }                    
The following is an example, which throws a division by zero exception and we catch it in catch block.
#include<iostream>
using namespace std;
int division(int n, int d)
{
 if(d==0){
 throw "EXCEPTION: / BY 0";
}
int res = n/d;
return res;
}
int main() {
try {
int r1 = division(10,2);
cout<<"RESULT1 IS : "<< r1<< endl;
int r2 = division(10,0);
cout<<"RESULT2 IS : "<< r2<< endl;
}
catch(const char * error) 
{
 cout<<"SOME ERROR FOUND"<< endl;
 cout<< error;
}
return 0;
}       
OUTPUT:
RESULT1 IS : 5
SOME ERROR FOUND
EXCEPTION: / BY 0

Next topic is Templates





 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved