Exceptions

How to handle the Exceptions in C++.
( try , catch....)
With Program
senexpertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
This is from the VC documentation.  Hopefully this should help. If not let me know and I'll try to add to it.

***********************

C++ exception handling uses the try, catch, and throw statements to implement exception handling. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code that is outside the normal flow of control.

Note   The Win32 structured exception-handling mechanism works with both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, throw) described in this topic.

try-block :
try compound-statement handler-list

handler-list :
handler handler-listopt

handler :
catch ( exception-declaration ) compound-statement

exception-declaration :
type-specifier-list declarator
type-specifier-list abstract-declarator
type-specifier-list
....

throw-expression :
throw assignment-expressionopt

The compound-statement after the try clause is the guarded section of code. The throw-expression "throws" (raises) an exception. The compound-statement after the catch clause is the exception handler, and "catches" (handles) the exception thrown by the throw-expression. The exception-declaration statement indicates the type of exception the clause handles. The type can be any valid data type, including a C++ class. If the exception-declaration statement is an ellipsis (...), the catch clause handles any type of exception, including a C exception. Such a handler must be the last handler for its try-block.

The operand of throw is syntactically similar to the operand of a return statement.

Note   Microsoft C++ does not support exception-specifications, as described in section 15.4 of the ANSI C++ draft. In addition, it does not support function-try-block described in section 15 of the ANSI C++ draft.

For more information on C++ exception handling, see Exception Handling Topics (C++). For information on exception handling in general, see Exception Handling: Overview.

END C++ Specific

Example

In the following example, the try block attempts to write the headers. The catch block then handles a specific file exception, and passes all other exceptions on to the outer block with the throw macro:

// Example of the try and catch statements
try
{
      // Write the file header
      file.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
      //
      // Write the DIB header and the bits
      file.WriteHuge(lpBI, dwDIBSize);
}
catch (CFileException* e)
   {
      ::GlobalUnlock((HGLOBAL) hDib);
      throw;
   }

::GlobalUnlock((HGLOBAL) hDib);
   return TRUE;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
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;
   }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.