| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
No matter how well a program is designed, if it has to interact with a user or other aspect of the outside world in any way, the code is bound to occasionally meet with cases that are either invalid or just plain unexpected. A very simple example is when a program asks the user to enter a filename, and the user enters the name of a file that does not exist, or does not enter a name at all. Perhaps a valid filename is entered, but, due to a previous disk write error the contents are garbled. Any number of things can go wrong. In addition, programmer error inevitably occurs and needs to be taken account of. Internal functions may be called with invalid arguments, either due to unexpected paths being taken through the code, or silly things like typos using the wrong variable for something. When these problems happen (and they will happen), it is better to handle them gracefully than for the program to crash, or worse, to continue processing but in an erroneous way.
To allow for this, many computer languages provide two types of facilities. The first is referred to as exception handling or sometimes error trapping. The second is referred to as assertion checking. Exceptions allow the program to catch errors when they occur and react to them explicitly. Assertions allow a programmer to establish that certain conditions hold before attempting to execute a particular operation. GNUstep provides both of these facilities, and we will cover each in turn. The assertion facility is tied in with the GNUstep logging facilities, so we describe those as well.
To use any of the facilities described in this chapter requires that you
include Foundation/NSException.h.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GNUstep exception handling provides for two things:
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
GNUstep exception handling is implemented through the macros NS_DURING,
NS_HANDLER, and NS_ENDHANDLER in conjunction with the
NSException class. The following illustrates the pattern:
NS_DURING
{
// do something risky ...
}
NS_HANDLER
{
// a problem occurred; inform user orce-and-Serialization" href="manual_8.html#Persistence-and-Serialization">8.7 Persistence and Serialization
This document was generated by root on November 21, 2014 using texi2html 1.82.
|