Showing posts with label Exception. Show all posts
Showing posts with label Exception. Show all posts

Is it possible that one try block can have multiple catch blocks?

There can be more than one exception handler (or catch block) for one try block, because it might so happen that in one single try block there can be different types of exceptions that can be thrown. 

This is shown in the following code snippet:
try
{
// statement that throws divide-by-zero exception
// statement that throws array out-of-bound exception
}
catch ( divide_error id1 ){ // code }
catch ( out_of_bound_error id2 ){ // code } 

Is it possible to throw an exception through a constructor?

Yes, we can! We cannot return any error value from the constructor, as the constructor doesn’t have any return type. In such situation, by throwing an exception we can pass value to catch block. 

This is shown in the following example:
#include <iostream.h>
class sample
{
     public:
           sample ( int i )
           {
                 if ( i == 0 ) throw "error"; 
           } 
};
void main( )
{
     try
     {
          sample s ( 0 );
     }
     catch ( char * str ) 
     {
          cout << str; 
     }

Exception Handling in C++

Exception:

An unwanted state during the execution of the program. It can also be defined as “errors at run-time”. 
For example, 
Dividing a number by zero.

Whenever there is an exception in the program, the programmer has two possible solutions: 

a)Resumption- fix the problem and continue
b)Termination-Abort the process and return

In C++,
the Resumption model is supported by the function call mechanism, and 
the Termination model is supported by the exception-handling mechanism

Exception Handling:

“Exception Handling” allows us to manage the exception gracefully. 
Using Exception Handling technique we can avoid abnormal termination of the program and continue with the normal flow or the program, or even move the control to other parts of the program.

Exceptions are handled using try catch statements.

General syntax: 
try{ 

statements;
. . . . . 


catch(type1 arg){statements;}

catch(type2 arg){statements;}

•We can have more than one catch statement associated with a try.


Exception Handling Example:

#include<iostream.h>
void main()

      int j; 
      cin>>j; 
      try
      { 
            if(j==0) throw j; 
               int i=20/j; 
      }catch(int j){ 
            cout<<"Divide by zero"; 
      }catch(...){ 
            cout<<"Caught Exception"; 
      }
}

Output: 
Divide by zero

Re-throwing an Exception:

A re-throw is indicated by a throw without an operand. 

Example;
try{ 
      //code which throws error 
}catch(Error e){ 
      throw; //re-throw 

If a re-throw is attempted when there is no exception to throw , terminate() will be called. 
terminate(), by default, calls abort() to stop the program. But we can specify our own termination handler.

Custom Exception Class:

#include<iostream.h> 
#include<string.h> 

class MyException

{           char *msg; 
          public: 
                  MyException(){
                          msg="MyException";
                  } 
                  MyException(char *m) 
                  {
                          msg=m;
                  } 
                  char* getMessage(){ 
                          return msg;
                  } 

};

void main() 

       int age; 
       try{ 
              cout<<"Enter your age"; 
              cin>>age; 
              if(age>18) 
                     cout<<"Ready to vote"; 
              else{ 
                     MyException e("You are Minor!!"); 
                     throw e;
              } 
       } 
       catch(MyException e) 
       { 
              cout<<"Exception"<<e.getMessage(); 
       }
}