Lecture 13: Exception Handling

Introduction to Handling Errors

Each program that you write should be designed to handle errors. One way is to let the program terminate without handling the error. This is in general an unsatisfactory approach. The error should be handled so that if the program must terminate, it terminates in an orderly manner. There are several ways to handle errors and the method depends on the type of error.

Programmers and in particular C or Classical C++ programmers typically handled errors by:

  • ignoring them

  • letting the program abort

  • testing for errors and if certain types occur use exit()

  • using errno when a particular error occurs

  • use assert in cassert with abort() in cstdlib

  • using setjump with longjump from setjump.h

  • using new_handler or set_new_handler if using new

If you are interested in the last four, I recommend you read a book on C because they will not be discussed in these lectures. Instead we will devote our time to a new way in Standard C++ that was created to handle errors called exception handling techniques. This lecture will only be an introduction to error handling with a brief introduction to the topic of exception handling of errors.

By exception handling is meant handing runtime errors caused by abnormal conditions. But these techniques do not handle all errors. C++ exception handling should not be used for:

  • disk I/O

  • network messages

  • mouse clicks

  • general responses to user input

  • errors that require the program to remain within the current scope

With exception handling, general errors are taken care of in a general way. These errors are not taken care at the location of where they occur. C++ exception handling should be used for synchronous errors like:

  • out of memory errors

  • arrays that are out of bounds

  • arithmetic overflow errors

  • division by zero errors

  • invalid function parameters

  • file I/O errors

  • but most importantly errors that permit the program to leave the current scope

Exception handing permits development teams to create sub teams who will work solely on error handling by using a better and more organized approach. These methods not only work in single threaded OS but also in multiple threaded OS like Windows and Unix. While error handling is an important feature that needs to be considered, the programmer must be careful not to overburden the program with an excessive amount of error handling. Where that line is between excessive and just right will only come with experience. It will also depend on the type of programs written.

Hopefully those who write programs for atomic power plants, heart monitors, aircraft guidance systems, corporate accounting systems and other critical software will use an excessive amount of error checking.

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net