Exceptions.h 7.6 KB

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