Error Reporting and Error Handling
Error reporting in C++ is often accomplished through the use of exceptions. When an error condition is encountered, an application can throw an exception to report the error condition, as follows:
 
if (errorCondition)
{
throw E;
}
“E” in the above throw statement can be any type. One may choose simply to throw an integer error code, or to use a customized error class providing detailed information regarding the error condition. When such exceptions are thrown and not specifically caught, the application in which the exception is thrown abnormally ends. To avoid abnormal program terminations, you should surround code that could throw an exception with a “try-catch” block, as follows:
 
try
{
possibleException();
}
catch (E e)
{
handleException(); }
The code that may throw an exception is placed within the try block. If an exception of type “E” is thrown, then it may be “caught” in the catch block that follows. If the type of expression in the catch statement is not the same as the type of a thrown exception (or is not a base class of the type of exception thrown), then the exception is not caught, and the program will terminate. If, however, the type of exception thrown does match that in the catch statement, then the code in the following catch block will be executed to handle the exception.