Vc++ 4(exception handling)


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Vc++ 4(exception handling)

  1. 1. Visual C++ Programming
  2. 2. Exceptions <ul><ul><li>Exceptions occur when a program executes abnormally because of conditions outside the program's control. </li></ul></ul><ul><ul><li>Exception can be defined as an abnormal event that occurs during the execution of a program and disrupts the normal flow of instructions. The abnormal event can also be an error in the program. </li></ul></ul><ul><ul><li>Certain operations, including object creation and file input/output, are subject to failures that go beyond errors. Out-of-memory conditions, for example, can occur even when your program is running correctly. </li></ul></ul>
  3. 3. Use of Exceptions <ul><li>The outcome of a function call during a program execution can be categorized as </li></ul><ul><ul><li>Normal Execution </li></ul></ul><ul><ul><ul><li>Occurs when a function executes normally and returns to a calling program. </li></ul></ul></ul><ul><ul><ul><li>Some functions return a result code to the caller and indicate success or failure (even a particular type of failure). </li></ul></ul></ul><ul><ul><li>Erroneous Execution </li></ul></ul><ul><ul><ul><li>Occurs when the caller makes a mistake in passing arguments or calls a function out of context. </li></ul></ul></ul><ul><ul><ul><li>The error which occurs can be detected by an assertion. </li></ul></ul></ul>
  4. 4. Use of Exceptions(contd..) <ul><ul><li>Abnormal Execution </li></ul></ul><ul><ul><ul><li>Includes situations where conditions outside the program’s control, influence the outcome of a function. </li></ul></ul></ul><ul><ul><ul><li>For eg. Insufficient memory, abnormality in I/O operations-(while opening a file which exists, the device driver for the storage media might fail.) </li></ul></ul></ul>
  5. 5. Exception Handling <ul><li>Visual C++ supports three kinds of exception handling: </li></ul><ul><li>C++ Exception Handling </li></ul><ul><ul><li>Although structured exception handling works with C and C++ source files, it is not specifically designed for C++. For C++ programs, C++ exception handling should be used. </li></ul></ul><ul><li>Structured Exception Handling </li></ul><ul><ul><li>Windows supplies its own exception mechanism, called SEH. It is not recommended for C++ or MFC programming. SEH should be used only in non-MFC C programs. </li></ul></ul><ul><li>MFC Exceptions </li></ul><ul><ul><li>Since version 3.0, MFC has used C++ exceptions but still supports its older exception handling macros, which are similar to C++ exceptions in form. </li></ul></ul>
  6. 6. Exception Handling(contd..) <ul><li>The try , throw , and catch statements implement exception handling. </li></ul><ul><li>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. </li></ul><ul><li>The C++ Exception Handling mechanism is flexible, since it can handle exceptions of any type. </li></ul><ul><li>The process of raising an exception is called Throwing an exception. </li></ul><ul><li>The C++ exception–handling model is non-reusable. Once the flow of program control has left the try block, it never returns to that block. </li></ul>
  7. 7. Process of Exception Handling <ul><li>The steps involved in the process of handling exceptions : </li></ul><ul><ul><li>Control reaches the try statement by normal sequential execution. The guarded section (within the try block) is executed. </li></ul></ul><ul><ul><li>If exception is not thrown during the execution of the guarded section, the catch clauses that follow the try block are not executed. The statements after the catch block are executed. </li></ul></ul><ul><ul><li>If an exception is thrown during the execution of the guarded section , a catch clause that can handle the exception is searched. </li></ul></ul><ul><ul><li>If a matching handler is not found, the predefined run-time function terminate is called. </li></ul></ul><ul><ul><li>If a matching catch is found, the parameters are initialized, and the instructions in the catch handler are executed.If the program terminates, all the values in the stack are released. </li></ul></ul>
  8. 8. Exception Handling in MFC <ul><li>MFC provides several predefined exception classes that are derived from CException class. </li></ul><ul><li>MFC functions throw exceptions of types derived from the CException class. To catch an exception thrown by a MFC function, a catch block with an argument that is a pointer to a CException object needs to be created. </li></ul>
  9. 9. Exception Handling in MFC(contd..) <ul><li>Some of the MFC Exception classes are: </li></ul>Exception Class Functionality CMemoryException Out-of-memory exception CFileException File-specific exception CArchiveException Archive/Serialization exception CDBException Database exceptions CResourceException Windows resource allocation exception