Exceptions.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //---------------------------------------------------------------------------
  2. #ifndef ExceptionsH
  3. #define ExceptionsH
  4. #include <Classes.hpp>
  5. #include <SysUtils.hpp>
  6. #include <SysInit.hpp>
  7. #include <System.hpp>
  8. //---------------------------------------------------------------------------
  9. bool __fastcall ShouldDisplayException(Exception * E);
  10. bool __fastcall ExceptionMessage(Exception * E, UnicodeString & Message);
  11. bool __fastcall ExceptionMessageFormatted(Exception * E, UnicodeString & Message);
  12. UnicodeString __fastcall LastSysErrorMessage();
  13. TStrings * __fastcall ExceptionToMoreMessages(Exception * E);
  14. //---------------------------------------------------------------------------
  15. enum TOnceDoneOperation { odoIdle, odoDisconnect, odoShutDown };
  16. //---------------------------------------------------------------------------
  17. class ExtException : public Sysutils::Exception
  18. {
  19. public:
  20. __fastcall ExtException(Exception* E);
  21. __fastcall ExtException(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = L"");
  22. // "copy the exception", just append message to the end
  23. __fastcall ExtException(UnicodeString Msg, Exception* E, UnicodeString HelpKeyword = L"");
  24. __fastcall ExtException(UnicodeString Msg, UnicodeString MoreMessages, UnicodeString HelpKeyword = "");
  25. __fastcall ExtException(UnicodeString Msg, TStrings* MoreMessages, bool Own, UnicodeString HelpKeyword = "");
  26. __fastcall virtual ~ExtException(void);
  27. __property TStrings* MoreMessages = {read=FMoreMessages};
  28. __property UnicodeString HelpKeyword = {read=FHelpKeyword};
  29. inline __fastcall ExtException(const UnicodeString Msg, const TVarRec * Args, const int Args_Size) :
  30. Sysutils::Exception(Msg, Args, Args_Size)
  31. {
  32. }
  33. inline __fastcall ExtException(int Ident, const TVarRec * Args, const int Args_Size)/* overload */ :
  34. Sysutils::Exception(Ident, Args, Args_Size)
  35. {
  36. }
  37. inline __fastcall ExtException(const UnicodeString Msg, int AHelpContext) :
  38. Sysutils::Exception(Msg, AHelpContext)
  39. {
  40. }
  41. inline __fastcall ExtException(const UnicodeString Msg, const TVarRec * Args, const int Args_Size, int AHelpContext) :
  42. Sysutils::Exception(Msg, Args, Args_Size, AHelpContext)
  43. {
  44. }
  45. inline __fastcall ExtException(int Ident, int AHelpContext)/* overload */ :
  46. Exception(Ident, AHelpContext)
  47. {
  48. }
  49. inline __fastcall ExtException(PResStringRec ResStringRec, const TVarRec * Args, const int Args_Size, int AHelpContext)/* overload */ :
  50. Sysutils::Exception(ResStringRec, Args, Args_Size, AHelpContext)
  51. {
  52. }
  53. virtual ExtException * __fastcall Clone();
  54. protected:
  55. void __fastcall AddMoreMessages(Exception* E);
  56. private:
  57. Classes::TStrings* FMoreMessages;
  58. UnicodeString FHelpKeyword;
  59. };
  60. //---------------------------------------------------------------------------
  61. #define DERIVE_EXT_EXCEPTION(NAME, BASE) \
  62. class NAME : public BASE \
  63. { \
  64. public: \
  65. inline __fastcall NAME(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = L"") : \
  66. BASE(E, Msg, HelpKeyword) \
  67. { \
  68. } \
  69. inline __fastcall NAME(Exception* E, int Ident) : \
  70. BASE(E, Ident) \
  71. { \
  72. } \
  73. inline __fastcall virtual ~NAME(void) \
  74. { \
  75. } \
  76. inline __fastcall NAME(const UnicodeString Msg, const TVarRec * Args, const int Args_Size) : \
  77. BASE(Msg, Args, Args_Size) \
  78. { \
  79. } \
  80. inline __fastcall NAME(int Ident, const TVarRec * Args, const int Args_Size) : \
  81. BASE(Ident, Args, Args_Size) \
  82. { \
  83. } \
  84. inline __fastcall NAME(const UnicodeString Msg, int AHelpContext) : \
  85. BASE(Msg, AHelpContext) \
  86. { \
  87. } \
  88. inline __fastcall NAME(const UnicodeString Msg, const TVarRec * Args, const int Args_Size, int AHelpContext) : \
  89. BASE(Msg, Args, Args_Size, AHelpContext) \
  90. { \
  91. } \
  92. inline __fastcall NAME(int Ident, int AHelpContext) : \
  93. BASE(Ident, AHelpContext) \
  94. { \
  95. } \
  96. inline __fastcall NAME(PResStringRec ResStringRec, const TVarRec * Args, const int Args_Size, int AHelpContext) : \
  97. BASE(ResStringRec, Args, Args_Size, AHelpContext) \
  98. { \
  99. } \
  100. virtual ExtException * __fastcall Clone() \
  101. { \
  102. return new NAME(this, L""); \
  103. } \
  104. };
  105. //---------------------------------------------------------------------------
  106. DERIVE_EXT_EXCEPTION(ESsh, ExtException);
  107. DERIVE_EXT_EXCEPTION(ETerminal, ExtException);
  108. DERIVE_EXT_EXCEPTION(ECommand, ExtException);
  109. DERIVE_EXT_EXCEPTION(EScp, ExtException); // SCP protocol fatal error (non-fatal in application context)
  110. DERIVE_EXT_EXCEPTION(EScpSkipFile, ExtException);
  111. DERIVE_EXT_EXCEPTION(EScpFileSkipped, EScpSkipFile);
  112. //---------------------------------------------------------------------------
  113. class EOSExtException : public ExtException
  114. {
  115. public:
  116. __fastcall EOSExtException();
  117. __fastcall EOSExtException(UnicodeString Msg);
  118. __fastcall EOSExtException(UnicodeString Msg, int LastError);
  119. };
  120. //---------------------------------------------------------------------------
  121. class EFatal : public ExtException
  122. {
  123. public:
  124. // fatal errors are always copied, new message is only appended
  125. __fastcall EFatal(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = "");
  126. __property bool ReopenQueried = { read = FReopenQueried, write = FReopenQueried };
  127. virtual ExtException * __fastcall Clone();
  128. private:
  129. bool FReopenQueried;
  130. };
  131. //---------------------------------------------------------------------------
  132. #define DERIVE_FATAL_EXCEPTION(NAME, BASE) \
  133. class NAME : public BASE \
  134. { \
  135. public: \
  136. inline __fastcall NAME(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = "") : \
  137. BASE(E, Msg, HelpKeyword) \
  138. { \
  139. } \
  140. virtual ExtException * __fastcall Clone() \
  141. { \
  142. return new NAME(this, L""); \
  143. } \
  144. };
  145. //---------------------------------------------------------------------------
  146. DERIVE_FATAL_EXCEPTION(ESshFatal, EFatal);
  147. //---------------------------------------------------------------------------
  148. // exception that closes application, but displays info message (not error message)
  149. // = close on completion
  150. class ESshTerminate : public EFatal
  151. {
  152. public:
  153. inline __fastcall ESshTerminate(Exception* E, UnicodeString Msg, TOnceDoneOperation AOperation) :
  154. EFatal(E, Msg),
  155. Operation(AOperation)
  156. {
  157. }
  158. virtual ExtException * __fastcall Clone();
  159. TOnceDoneOperation Operation;
  160. };
  161. //---------------------------------------------------------------------------
  162. class ECallbackGuardAbort : public EAbort
  163. {
  164. public:
  165. __fastcall ECallbackGuardAbort();
  166. };
  167. //---------------------------------------------------------------------------
  168. Exception * __fastcall CloneException(Exception * Exception);
  169. void __fastcall RethrowException(Exception * E);
  170. UnicodeString __fastcall GetExceptionHelpKeyword(Exception * E);
  171. UnicodeString __fastcall MergeHelpKeyword(const UnicodeString & PrimaryHelpKeyword, const UnicodeString & SecondaryHelpKeyword);
  172. bool __fastcall IsInternalErrorHelpKeyword(const UnicodeString & HelpKeyword);
  173. //---------------------------------------------------------------------------
  174. #endif // Exceptions