What is exception? Why exception handling is better to use? Explain exception handling with try….. catch by using suitable example.

This answer is restricted. Please login to view the answer of this question.

Login Now

Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution.

It is better to have exception handling because of following reason

  1. Exception handling can control run tune errors that occur in the program.
  2. It can avoid abnormal termination of the program and also shows the behavior of program to users.
  3. It can provide a facility to handle exceptions, throws message regarding exception and completes the execution of program by catching the exception
  4. It can separate the error handling code and normal code by using try-catch block.
  5. It can produce the normal execution flow for a program.
  6. It can implement a clean way to propagate error. that is. when an invoking method cannot manage a particular situations, then it throws an exception and asks the invoking method to deal with such situation.
  7.  It develops a powerful coding which ensures that the exceptions can be prevented.
  8. It also allows to handle related exceptions by single exception handler. All the related errors are grouped together by using exceptions. And then they are handled by using single exception handler.

Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon 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.

Synatx:

try {
   // protected code
} catch( ExceptionName e1 ) {
   // catch block
} catch( ExceptionName e2 ) {
   // catch block
} catch( ExceptionName eN ) {
   // catch block
}

Let’s take an example

#include <iostream>
using namespace std;

double division(int a, int b) {
   if( b == 0 ) {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main () {
   int x = 50;
   int y = 0;
   double z = 0;
 
   try {
      z = division(x, y);
      cout << z << endl;
   } catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

Because we are raising an exception of type const char*, so while catching this exception, we have to use const char* in catch block. If we compile and run above code, this would produce the following result −

Division by zero condition!
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .