Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 17: Exception Handling in C++

Chapter 17: Exception Handling in C++

pp. 969-993

Authors

, Vidyalankar Institute of Technology
  • Add bookmark
  • Cite
  • Share

Summary

Overview

Exception is an abnormal situation in the program that can terminate the program if it is not handled by the programmer. Some examples of exception are as follows:

  • 1. If you accidently perform division by zero in the program, then it is called division by zero exception

  • 2. If you access array elements beyond the specific boundaries of an array index, then it is called array index out of bounds exception. For example, if the size of array a is 5 and if you accidently try to access a[7] that does not exist, then it could give logically incorrect results, which is an exception

  • 3. If your program is required to take salary of an employee as input from the user and by mistake the user inputs a negative number, then it is called an invalid input exception

  • 4. If your program is writing to a file but the file is read-only, then there is an access rights violation in performing the output operation, which is also an example of exception

  • 5. In network communication, if you are writing your own C++ client to communicate with the server on the same network and if server is down when you are sending messages through the client program, then it is called ‘Resource Not available’ type of exception

  • There are many more specific types of exceptions that may occur in your program from case to case. What is important is to understand what exceptions are and how effectively we can handle them to ensure that the program does not terminate abruptly when exception occurs. It is the programmer's responsibility to create robust programs that do not fail abruptly even if any abnormal situation occurs.

    NOTES

    There is a difference between an exception and an error. If there is an error in the program, the program will certainly fail. For example, a semicolon missing after a statement is an example of syntax error; program going out of memory while running a large loop is an example of memory error and so on. Errors cannot be handled and hence the program will certainly fail when an error occurs. Whereas, exception is something that can be handled in the program thereby avoiding the abrupt failure of the program. Exception handlers in C++ are used to handle exceptions (not errors).

    About the book

    Access options

    Review the options below to login to check your access.

    Purchase options

    eTextbook
    US$110.00
    Paperback
    US$110.00

    Have an access code?

    To redeem an access code, please log in with your personal login.

    If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

    Also available to purchase from these educational ebook suppliers