#Previous question #Index of C++ Questions #Next question

Exception handling is a nice and transparent way to handle errors in programs. For exception handling in C++, the block code to be written is enclosed in a ‘try’ block. Within the ‘try’ block, we can throw any occurring errors with ‘throw’ command. Immediately after the ‘try’ block, ‘catch’ block starts wherein error handling code is placed. Thus we can say that there are three keywords for exception handling in C++.

These are:
Try: A ‘try’ block is a group of C++ statement enclosed in curly braces {}, which may cause (or throw) an exception.
Catch: A ‘catch’ block is a group of C++ statement that is used to handle a specific raised exception. ‘Catch’ block should be laced after each ‘Try’ block. A ‘Catch’ block is specified by the following:

  • The keyword ‘catch’
  • A catch expression, which corresponds to a specific type of exception that may be thrown by the ‘Try’ block.
  • A group of statements enclosed within braces{} to handle the exception
Throw: This statement is used to throw an exception. A ‘Throw’ statement specified with:
  • The keyword ‘throw’
  • An assignment expression; the type of the result of this expression determines which catch bock receives control.

Format:

try
{
 //: throw exception;
}
catch(type exception)
{
  //code to be executed in 
  case of exception. 
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students