[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6. Exception Handling, Logging, and Assertions

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] [ ? ]

6.1 Exceptions

GNUstep exception handling provides for two things:

  1. When an error condition is detected during execution, control is passed to a special error-handling routine, which is given information on the error that occurred.
  2. This routine may itself, if it chooses, pass this information up the function call stack to the next higher level of control. Often higher level code is more aware of the context in which the error is occurring, and can therefore make a better decision as to how to react.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1.1 Catching and Handling Exceptions

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
    
    
  • 8.8 Utility
  • 8.9 Notifications
  • 8.10 Networking and RPC
  • 8.11 Threads and Run Control
  • 8.12 GNUstep Additions
  • A. The GNUstep Documentation System
  • B. Application Resources: Bundles and Frameworks
  • C. Differences and Similarities Between Objective-C, Java, and C++
  • D. Programming GNUstep in Java and Guile
  • E. GNUstep Compliance to Standards
  • F. Using the GNUstep Make Package
  • Concept Index

  • [Top] [Contents] [Index] [ ? ]

    This document was generated by root on November 21, 2014 using texi2html 1.82.

    ./usr/share/GNUstep/Documentation/Developer/Base/ProgrammingManual/manual_6.html0000644000000000000000000014570412433617271026612 0ustar rootroot Objective-C GNUstep Base Programming Manual: 6. Exception Handling, Logging, and Assertions
    [ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

    6. Exception Handling, Logging, and Assertions

    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] [ ? ]

    6.1 Exceptions

    GNUstep exception handling provides for two things:

    1. When an error condition is detected during execution, control is passed to a special error-handling routine, which is given information on the error that occurred.
    2. This routine may itself, if it chooses, pass this information up the function call stack to the next higher level of control. Often higher level code is more aware of the context in which the error is occurring, and can therefore make a better decision as to how to react.

    [ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

    6.1.1 Catching and Handling Exceptions

    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
        
        
  • 8.8 Utility
  • 8.9 Notifications
  • 8.10 Networking and RPC
  • 8.11 Threads and Run Control
  • 8.12 GNUstep Additions
  • A. The GNUstep Documentation System
  • B. Application Resources: Bundles and Frameworks
  • C. Differences and Similarities Between Objective-C, Java, and C++
  • D. Programming GNUstep in Java and Guile
  • E. GNUstep Compliance to Standards
  • F. Using the GNUstep Make Package
  • Concept Index

  • [Top] [Contents] [Index] [ ? ]

    This document was generated by root on November 21, 2014 using texi2html 1.82.

    ./usr/share/GNUstep/Documentation/Developer/Base/ProgrammingManual/manual_6.html0000644000000000000000000014570412433617271026612 0ustar rootroot Objective-C GNUstep Base Programming Manual: 6. Exception Handling, Logging, and Assertions
    [ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

    6. Exception Handling, Logging, and Assertions

    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] [ ? ]

    6.1 Exceptions

    GNUstep exception handling provides for two things:

    1. When an error condition is detected during execution, control is passed to a special error-handling routine, which is given information on the error that occurred.
    2. This routine may itself, if it chooses, pass this information up the function call stack to the next higher level of control. Often higher level code is more aware of the context in which the error is occurring, and can therefore make a better decision as to how to react.

    [ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

    6.1.1 Catching and Handling Exceptions

    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
        
        
  • 8.8 Utility
  • 8.9 Notifications
  • 8.10 Networking and RPC
  • 8.11 Threads and Run Control
  • 8.12 GNUstep Additions
  • A. The GNUstep Documentation System
  • B. Application Resources: Bundles and Frameworks
  • C. Differences and Similarities Between Objective-C, Java, and C++
  • D. Programming GNUstep in Java and Guile
  • E. GNUstep Compliance to Standards
  • F. Using the GNUstep Make Package
  • Concept Index

  • [Top] [Contents] [Index] [ ? ]

    This document was generated by root on November 21, 2014 using texi2html 1.82.

    ./usr/share/GNUstep/Documentation/Developer/Base/ProgrammingManual/manual_6.html0000644000000000000000000014570412433617271026612 0ustar rootroot Objective-C GNUstep Base Programming Manual: 6. Exception Handling, Logging, and Assertions
    [ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [