Exception Handling

C++ program uses error-handling techniques to resolve error conditions. In addition to throwing and catching exceptions, we also examine how exception specifications are declared and exception classes are defined, additionally looking into the use of standard exception classes.

Traditional Error Handling

Error checking after leaving a function

 

Error Conditions

Errors that occur at program runtime can seriously interrupt the normal flow of a program. Some common causes of errors are
  • division by 0, or values that are too large or small for a type
  • no memory available for dynamic allocation
  • errors on file access, for example, file not found
  • attempt to access an invalid address in main memory
  • invalid user input
Anomalies like these lead to incorrect results and may cause a computer to crash. Both of these cases can have fatal effects on your application.
One of the programmer's most important tasks is to predict and handle errors. You can judge a program's quality by the way it uses error-handling techniques to counteract any potential error, although this is by no means easy to achieve.

Traditional Error Handling

Traditional structured programming languages use normal syntax to handle errors:
  • errors in function calls are indicated by special return values
  • global error variables or flags are set when errors occur, and then checked again later.
If a function uses its return value to indicate errors, the return value must be examined whenever the function is called, even if no error has occurred.
Example:
if( func()> 0 )
   // Return value positive => o.k.
else
   // Treat errors 
Error variables and flags must also be checked after every corresponding action.
In other words, you need to continually check for errors while a program is executing. If you do happen to forget to check for errors, the consequences may be fatal.

Post a Comment

0 Comments