jexcept.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __JEXCEPT__
  14. #define __JEXCEPT__
  15. #include "jiface.hpp"
  16. #include "jlib.hpp"
  17. #include "errno.h"
  18. jlib_decl const char* SerializeMessageAudience(MessageAudience ma);
  19. jlib_decl MessageAudience DeserializeMessageAudience(const char* text);
  20. //the following interface to be thrown when a user command explicitly calls for a failure
  21. interface jlib_thrown_decl IUserException : public IException
  22. {
  23. };
  24. //the following interface defines a collection of exceptions
  25. //
  26. interface jlib_thrown_decl IMultiException : extends IException
  27. {
  28. //convenience methods for handling this as an array
  29. virtual aindex_t ordinality() const = 0;
  30. virtual IException& item(aindex_t pos) const = 0;
  31. virtual const char* source() const = 0;
  32. //for complete control...
  33. virtual IArrayOf<IException>& getArray()= 0;
  34. //add another exception
  35. virtual void append(IException& e) = 0;
  36. virtual void append(IMultiException& e) = 0;
  37. virtual StringBuffer& serialize(StringBuffer& ret, unsigned indent = 0, bool simplified=false, bool root=true) const = 0;
  38. virtual void deserialize(const char* xml) = 0; //throws IException on failure!
  39. //the following methods override those in IIException
  40. //
  41. virtual int errorCode() const = 0;
  42. virtual StringBuffer& errorMessage(StringBuffer &msg) const = 0;
  43. virtual MessageAudience errorAudience() const = 0;
  44. };
  45. IMultiException jlib_decl *MakeMultiException(const char* source = NULL);
  46. interface IExceptionHandler
  47. {
  48. virtual bool fireException(IException *e) = 0;
  49. };
  50. IException jlib_decl *MakeStringException(int code,const char *why, ...) __attribute__((format(printf, 2, 3)));
  51. IException jlib_decl *MakeStringExceptionVA(int code,const char *why, va_list args);
  52. IException jlib_decl *MakeStringExceptionDirect(int code,const char *why);
  53. IException jlib_decl *MakeStringException(MessageAudience aud,int code,const char *why, ...) __attribute__((format(printf, 3, 4)));
  54. IException jlib_decl *MakeStringExceptionVA(MessageAudience aud,int code,const char *why, va_list args);
  55. IException jlib_decl *MakeStringExceptionDirect(MessageAudience aud,int code,const char *why);
  56. void jlib_decl ThrowStringException(int code,const char *format, ...) __attribute__((format(printf, 2, 3), noreturn));
  57. interface jlib_thrown_decl IOSException: extends IException{};
  58. IOSException jlib_decl *MakeOsException(int code);
  59. IOSException jlib_decl *MakeOsException(int code, const char *msg, ...) __attribute__((format(printf, 2, 3)));
  60. #define DISK_FULL_EXCEPTION_CODE ENOSPC
  61. interface jlib_thrown_decl IErrnoException: extends IException{};
  62. IErrnoException jlib_decl *MakeErrnoException(int errn=-1);
  63. IErrnoException jlib_decl *MakeErrnoException(int errn, const char *why, ...) __attribute__((format(printf, 2, 3)));
  64. IErrnoException jlib_decl *MakeErrnoException(const char *why, ...) __attribute__((format(printf, 1, 2)));
  65. IErrnoException jlib_decl *MakeErrnoException(MessageAudience aud,int errn=-1);
  66. IErrnoException jlib_decl *MakeErrnoException(MessageAudience aud,int errn, const char *why, ...) __attribute__((format(printf, 3, 4)));
  67. IErrnoException jlib_decl *MakeErrnoException(MessageAudience aud,const char *why, ...) __attribute__((format(printf, 2, 3)));
  68. void jlib_decl pexception(const char *msg,IException *e); // like perror except for exceptions
  69. jlib_decl StringBuffer & formatSystemError(StringBuffer & out, unsigned errcode);
  70. void userBreakpoint();
  71. interface jlib_thrown_decl ISEH_Exception : extends IException
  72. {
  73. };
  74. interface jlib_thrown_decl IOutOfMemException: extends IException
  75. {
  76. };
  77. void jlib_decl EnableSEHtoExceptionMapping(); // return value can be used to disable
  78. void jlib_decl DisableSEHtoExceptionMapping();
  79. // NB only enables for current thread or threads started after call
  80. // requires /EHa option to be set in VC++ options (after /GX)
  81. void jlib_decl *setSEHtoExceptionHandler(IExceptionHandler *handler); // sets handler and return old value
  82. void jlib_decl setTerminateOnSEHInSystemDLLs(bool set=true);
  83. void jlib_decl setTerminateOnSEH(bool set=true);
  84. #define makeUnexpectedException() MakeStringException(9999, "Internal Error at %s(%d)", __FILE__, __LINE__)
  85. #define throwUnexpected() throw MakeStringException(9999, "Internal Error at %s(%d)", __FILE__, __LINE__)
  86. #define throwUnexpectedX(x) throw MakeStringException(9999, "Internal Error '" x "' at %s(%d)", __FILE__, __LINE__)
  87. #define assertThrow(x) assertex(x)
  88. #define UNIMPLEMENTED throw MakeStringException(-1, "UNIMPLEMENTED feature at %s(%d)", __FILE__, __LINE__)
  89. #define UNIMPLEMENTED_X(reason) throw MakeStringException(-1, "UNIMPLEMENTED '" reason "' at %s(%d)", __FILE__, __LINE__)
  90. #define UNIMPLEMENTED_XY(a,b) throw MakeStringException(-1, "UNIMPLEMENTED " a " %s at %s(%d)", b, __FILE__, __LINE__)
  91. IException jlib_decl * deserializeException(MemoryBuffer & in);
  92. void jlib_decl serializeException(IException * e, MemoryBuffer & out);
  93. void jlib_decl PrintStackReport();
  94. #ifdef _DEBUG
  95. #define RELEASE_CATCH_ALL int*********
  96. #else
  97. #define RELEASE_CATCH_ALL ...
  98. #endif
  99. //These are used in several places to wrap error reporting, to keep error numbers+text together. E.g.,
  100. //#define XYZfail 99 #define XXZfail_Text "Failed" throwError(XYZfail)
  101. #define throwError(x) ThrowStringException(x, (x ## _Text))
  102. #define throwError1(x,a) ThrowStringException(x, (x ## _Text), a)
  103. #define throwError2(x,a,b) ThrowStringException(x, (x ## _Text), a, b)
  104. #define throwError3(x,a,b,c) ThrowStringException(x, (x ## _Text), a, b, c)
  105. #define throwError4(x,a,b,c,d) ThrowStringException(x, (x ## _Text), a, b, c, d)
  106. #endif