Exceptions.h 7.5 KB

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