ErrorHandling.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. #ifndef kGenericError
  5. #define kGenericError -1
  6. #endif
  7. extern char *gErrorMessage;
  8. void SetErrorMessage(const char *theErrorMessage);
  9. void SetErrorMessageAndAppendLongInt(const char *theErrorMessage,const long theLongInt);
  10. void SetErrorMessageAndCStrAndLongInt(const char *theErrorMessage,const char * theCStr,const long theLongInt);
  11. void SetErrorMessageAndCStr(const char *theErrorMessage,const char * theCStr);
  12. void AppendCStrToErrorMessage(const char *theErrorMessage);
  13. void AppendLongIntToErrorMessage(const long theLongInt);
  14. char *GetErrorMessage(void);
  15. OSErr GetErrorMessageInNewHandle(Handle *inoutHandle);
  16. OSErr GetErrorMessageInExistingHandle(Handle inoutHandle);
  17. OSErr AppendErrorMessageToHandle(Handle inoutHandle);
  18. #ifdef __EXCEPTIONS_ENABLED__
  19. void ThrowErrorMessageException(void);
  20. #endif
  21. // A bunch of evil macros that would be unnecessary if I were always using C++ !
  22. #define SetErrorMessageAndBailIfNil(theArg,theMessage) \
  23. { \
  24. if (theArg == nil) \
  25. { \
  26. SetErrorMessage(theMessage); \
  27. errCode = kGenericError; \
  28. goto EXITPOINT; \
  29. } \
  30. }
  31. #define SetErrorMessageAndBail(theMessage) \
  32. { \
  33. SetErrorMessage(theMessage); \
  34. errCode = kGenericError; \
  35. goto EXITPOINT; \
  36. }
  37. #define SetErrorMessageAndLongIntAndBail(theMessage,theLongInt) \
  38. { \
  39. SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \
  40. errCode = kGenericError; \
  41. goto EXITPOINT; \
  42. }
  43. #define SetErrorMessageAndLongIntAndBailIfError(theErrCode,theMessage,theLongInt) \
  44. { \
  45. if (theErrCode != noErr) \
  46. { \
  47. SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \
  48. errCode = theErrCode; \
  49. goto EXITPOINT; \
  50. } \
  51. }
  52. #define SetErrorMessageCStrLongIntAndBailIfError(theErrCode,theMessage,theCStr,theLongInt) \
  53. { \
  54. if (theErrCode != noErr) \
  55. { \
  56. SetErrorMessageAndCStrAndLongInt(theMessage,theCStr,theLongInt); \
  57. errCode = theErrCode; \
  58. goto EXITPOINT; \
  59. } \
  60. }
  61. #define SetErrorMessageAndCStrAndBail(theMessage,theCStr) \
  62. { \
  63. SetErrorMessageAndCStr(theMessage,theCStr); \
  64. errCode = kGenericError; \
  65. goto EXITPOINT; \
  66. }
  67. #define SetErrorMessageAndBailIfError(theErrCode,theMessage) \
  68. { \
  69. if (theErrCode != noErr) \
  70. { \
  71. SetErrorMessage(theMessage); \
  72. errCode = theErrCode; \
  73. goto EXITPOINT; \
  74. } \
  75. }
  76. #define SetErrorMessageAndLongIntAndBailIfNil(theArg,theMessage,theLongInt) \
  77. { \
  78. if (theArg == nil) \
  79. { \
  80. SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \
  81. errCode = kGenericError; \
  82. goto EXITPOINT; \
  83. } \
  84. }
  85. #define BailIfError(theErrCode) \
  86. { \
  87. if ((theErrCode) != noErr) \
  88. { \
  89. goto EXITPOINT; \
  90. } \
  91. }
  92. #define SetErrCodeAndBail(theErrCode) \
  93. { \
  94. errCode = theErrCode; \
  95. \
  96. goto EXITPOINT; \
  97. }
  98. #define SetErrorCodeAndMessageAndBail(theErrCode,theMessage) \
  99. { \
  100. SetErrorMessage(theMessage); \
  101. errCode = theErrCode; \
  102. goto EXITPOINT; \
  103. }
  104. #define BailNow() \
  105. { \
  106. errCode = kGenericError; \
  107. goto EXITPOINT; \
  108. }
  109. #ifdef __cplusplus
  110. }
  111. #endif