Link to home
Start Free TrialLog in
Avatar of senexpert
senexpert

asked on

Exceptions

How to handle the Exceptions in C++.
( try , catch....)
With Program
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KangaRoo
KangaRoo

void f()
{

  // if a situation occurs that can't be handled you
  // can throw an exception object. Any object can
  // be trown. Noramlly you create different
  // exception classes for different types of errors
  if (some_error == true)
     throw xmsg("Something terrible happened");
  if (some_other_error == true)
     thow "Oeps";

}

void g()
{
   // prepare to catch execptions
   // we 'try' if we can call f()
   try
   {
      f();
   }
   // when f() throws an exception, it can be caught
   // and handled by special code:
   catch(xmsg& x)
   {
       cout << x.why() << end;      
   }
   catch(char* c)
   {
      cout << c << endl;
   }
   // all exceptions that slipped passed the
   // previous handlers are caught here
   catch(...)
   {
      cout << "Unknown Exception caught" << endl;
   }
}