home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Headers / objc / error.h next >
Text File  |  1991-11-26  |  4KB  |  108 lines

  1.  
  2. /*
  3.     error.h
  4.  
  5.     This file defines the interface to the exception raising scheme.
  6.  
  7.     Copyright (c) 1988 NeXT, Inc. as an unpublished work.
  8.     All rights reserved.
  9. */
  10.  
  11. #ifndef _OBJC_ERROR_H_
  12. #define _OBJC_ERROR_H_
  13.  
  14. #include <setjmp.h>
  15.  
  16. typedef struct _NXHandler {    /* a node in the handler chain */
  17.     jmp_buf jumpState;            /* place to longjmp to */
  18.     struct _NXHandler *next;        /* ptr to next handler */
  19.     int code;                /* error code of exception */
  20.     const void *data1, *data2;        /* blind data for describing error */
  21. } NXHandler;
  22.  
  23.  
  24. /* Handles RAISE's with nowhere to longjmp to */
  25. typedef void NXUncaughtExceptionHandler(int code, const void *data1,
  26.                         const void *data2);
  27. extern NXUncaughtExceptionHandler *_NXUncaughtExceptionHandler;
  28. #define NXGetUncaughtExceptionHandler() _NXUncaughtExceptionHandler
  29. #define NXSetUncaughtExceptionHandler(proc) \
  30.             (_NXUncaughtExceptionHandler = (proc))
  31.  
  32. /* NX_DURING, NX_HANDLER and NX_ENDHANDLER are always used like:
  33.  
  34.     NX_DURING
  35.         some code which might raise an error
  36.     NX_HANDLER
  37.         code that will be jumped to if an error occurs
  38.     NX_ENDHANDLER
  39.  
  40.    If any error is raised within the first block of code, the second block
  41.    of code will be jumped to.  Typically, this code will clean up any
  42.    resources allocated in the routine, possibly case on the error code
  43.    and perform special processing, and default to RERAISE the error to
  44.    the next handler.  Within the scope of the handler, a local variable
  45.    called NXLocalHandler of type NXHandler holds information about the
  46.    error raised.
  47.  
  48.    It is illegal to exit the first block of code by any other means than
  49.    NX_VALRETURN, NX_VOIDRETURN, or just falling out the bottom.
  50.  */
  51.  
  52. /* private support routines.  Do not call directly. */
  53. extern void _NXAddHandler( NXHandler *handler );
  54. extern void _NXRemoveHandler( NXHandler *handler );
  55. extern _setjmp(jmp_buf env);
  56.  
  57. #define NX_DURING { NXHandler NXLocalHandler;            \
  58.             _NXAddHandler(&NXLocalHandler);        \
  59.             if( !_setjmp(NXLocalHandler.jumpE(e) ) {
  60.  
  61. #define NX_HANDLER _NXRemoveHandler(&NXLocalHandler); } else {
  62.  
  63. #define NX_ENDHANDLER }}
  64.  
  65. #define NX_VALRETURN(val)  do { typeof(val) temp = (val);    \
  66.             _NXRemoveHandler(&NXLocalHandler);    \
  67.             return(temp); } while (0)
  68.  
  69. #define NX_VOIDRETURN    do { _NXRemoveHandler(&NXLocalHandler);    \
  70.             return; } while (0)
  71.  
  72. /* RAISE and RERAISE are called to indicate an error condition.  They
  73.    initiate the process of jumping up the chain of handlers.
  74.  */
  75.  
  76. #ifdef __GNUC__
  77. #ifndef __STRICT_ANSI__
  78. __volatile    /* never returns */
  79. #endif /* not __STRICT_ANSI__ */
  80. #endif /* __GNUC__ */
  81. extern void _NXRaiseError(int code, const void *data1, const void *data2);
  82.  
  83. #define NX_RAISE( code, data1, data2 )    \
  84.         _NXRaiseError( (code), (data1), (data2) )
  85.  
  86. #define NX_RERAISE()     _NXRaiseError( NXLocalHandler.code,    \
  87.                 NXLocalHandler.data1, NXLocalHandler.data2 )
  88.  
  89. /* These routines set and return the procedure which is called when
  90.    exceptions are raised.  This procedure must NEVER return.  It will
  91.    usually either longjmp, or call the uncaught exception handler.
  92.    The default exception raiser is also declared
  93.  */
  94. typedef void NXExceptionRaiser(int code, const void *data1, const void *data2);
  95. extern void NXSetExceptionRaiser(NXExceptionRaiser *proc);
  96. extern NXExceptionRaiser *NXGetExceptionRaiser(void);
  97. extern NXExceptionRaiser NXDefaultExceptionRaiser;
  98.  
  99.  
  100. /* The error buffer is used to allocate data which is passed up to other
  101.    handlers.  Clients should clear the error buffer in their top level
  102.    handler.  The Application Kit does this.
  103.  */
  104. extern void NXAllocErrorData(int size, void **data);
  105. extern void NXResetErrorData(void);
  106.  
  107. #endif /* _OBJC_ERROR_H_ */
  108.