What is exception handling?   In programming two most common types of error are logical error and syntax error.  The syntax error is de...

Exception handling and Namespace in C++

/
2 Comments
What is exception handling? 

  •  In programming two most common types of error are logical error and syntax error.
  •  The syntax error is detected during compilation of the program, but the logical error will detect during execution of the program. So, it is very difficult to handle the logical error. • The exception handling provides the mechanism to handle the logical error during execution of the program.
  • Steps to handle the logical error:

           1.Find the problem (Hit the exception)
           2.Inform that an error has occurred (Throw the exception)
           3.Receive the error information (Catch the exception)
           4.Take corrective actions (Handle the exception)

Exception Handling Mechanism: 

  •  This mechanism is built upon three keywords: try, throw and catch. 
  •  try is used to preface a block of statement which may generate exceptions. 
  • When an exception is detected, it is thrown using a throw statement. 
  •  A catch block is used to catches the exceptions thrown by throw statement and take appropriate action. 
  • •The relationship between try, throw and catch block is as shown in below figure. 

  • Syntax: 
            try 
           {
           // Set of Statments; 
          throw exception; 
          // Set of Statements; 
        }
        catch (type arg)
      { 
       // Set of Statements;
      } 


  • Example;
          #include<iostream.h>
         #include<conio.h>
         void main()
        {
         int a,b;
         cout<<"enter a and b";
         cin>>a>>b;
         try{
         if(b!=0)
          {
          cout<<"a/b ="<<a/b;
           }
           else{
            throw (b);
           }
           }
          catch(int x)
           {
            cout<<"exeception"<<x;
           }
          getch();
           }

  •  When the value of b is zero at that time exception will throw and this exception will catch in catch block and print the message value is zero. 


      What is multiple catch statements?
  •  It is possible that a program segment has more than one condition to throw an exception. 
  •  For these situations, we can associate more than one catch statement with a try block. 
  • When an exception is thrown, the exception handlers are searched in order for an appropriate match. 
  • The first handler that yields a match is executed. 
  • After executing the handler, the control goes to the first statement after the last catch block for that try. 
  •  Note: It is possible that arguments of several catch statements match the type of an exception. 
  • In such cases, the first handler that matches the exception type is executed. 
Example: 
          #include<iostream.h>
          #include<conio.h>
          void main()
         {
           int x;
           cout<<"enter value of X";
           cin>>x;
       try{
         if(x==1)
         {
           thow(x);
       }
        else if(x==0)
       {
          thow'x';
         }
     else if(x==-1)
     {
        thow 1.0;
     }
    }
       catch(char x)
   {
     cout<<"exeception"<<x;
    }
       catch(int x)
      {
     cout<<"exeception"<<x;
      }
    catch(float x)
    {
     cout<<"exeception"<<x;
     }
    getch();
   }

What is Rethrowing an Exception?
  • A handler may decide to rethrow the exception caught without processing it. 
  • In such situations, we may simply invoke throw without any arguments as shown below: 
         throw ; 
  • This causes the current exception to be thrown to the next enclosing try/catch sequence and is caught by 
    a catch statement listed after that enclosing try block. 
  • Example: 
      #include<iostream.h>
        #include<conio.h>
       void divide (double x, double y) 
       {
        cout<<"inside function"<<endl;
        try
       {
         if(y==0)
         throw y;
        else
         cout<<"Division="<<x/y<<endl;
       }
      catch(double)
     {
       cout<<"caught double inside te function\n";
     throw;
       }
       cout<<"Exnd of function\n\n";
      }
    void main()
   {
    cout<<"inside main";
      try{
     divide(10.5,2.5);
     divide(4,0);
     }
  catch(double)
  {
   cout<<"catch double inside";
     }
    cout<<"main end \n";
     getch();
   }

What is namespace?
  • Namespaces provide a method for preventing name conflicts in large projects.
  • Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.
  • Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
  • A namespace works to avoid names conflicts, for example, the standard library definessort() that is a really good name for a sorting function, thanks to namespaces you can define your own becausesort() it won't be in the same namespace as the standard one.
  • In namespace, we can declare the function, variable etc but we cannot use it
  • We cannot call the variable of function in namespace we can just declare it.
  • Because memory is allocated to the object, not to the namespace of class.
  • When we use namespace in our program we must define all that variable otherwise that class become the abstract class.
  • It means we can not create an object of that class then memory will not be allocated to that class.
  • we can declare namespace using namespace keyword.
  • Syntax:
             namespace namspace-name
            {
                 //decleration
                //decleration
              }

Example:

      // namespaces
      #include <iostream.h>
     #include<conio.h>

    namespace a // namespace a declaration 
   {
    int var = 5;
  }

     namespace b   //namespace b declaration 

       {
       double var = 3.1416;
      }

    void main () {

     cout << a::var << endl; // accessing variable var of namespace (a) using scpoe resolution(::)
     cout << b::var << endl;  // accessing variable var of namespace b) using scpoe resolution(::)

    getch();
     }





You may also like

2 comments:

  1. Contact: elizabethjone146@gmail.com
    WhatsApp +18572012269
    if you have any problem when it comes to-
    - Phone hack
    - Delete records
    - Improve credit score
    - improve poor grades
    _ gmail facebook
    _ twiter hotmail
    _ aol database
    I have used there service in gaining access to my ex boy friend who has been trying to blackmail me.
    so am talking from experience of there service, it works for real and i trust there service 100%

    ReplyDelete
  2. The King Casino | Situs Judi Slot Online Terbaik 2021
    Play online 1xbet korean Pragmatic Play Slots at The https://octcasino.com/ King Casino - worrione Member Baru & Terpercaya 2021! Rating: 98% · https://septcasino.com/review/merit-casino/ ‎240,388 votes

    ReplyDelete

Cybercry 2018. Powered by Blogger.

Popular Posts