Exceptions.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(NativeUInt 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(NativeUInt 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. virtual void __fastcall Rethrow();
  59. protected:
  60. void __fastcall AddMoreMessages(Exception* E);
  61. private:
  62. Classes::TStrings* FMoreMessages;
  63. UnicodeString FHelpKeyword;
  64. };
  65. //---------------------------------------------------------------------------
  66. #define EXT_EXCEPTION_METHODS(NAME, BASE) \
  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 NAME(const UnicodeString & Msg, const UnicodeString & MoreMessages, const UnicodeString & HelpKeyword = UnicodeString()) : \
  77. BASE(Msg, MoreMessages, HelpKeyword) \
  78. { \
  79. } \
  80. inline __fastcall virtual ~NAME(void) \
  81. { \
  82. } \
  83. inline __fastcall NAME(const UnicodeString Msg, const TVarRec * Args, const int Args_Size) : \
  84. BASE(Msg, Args, Args_Size) \
  85. { \
  86. } \
  87. inline __fastcall NAME(NativeUInt Ident, const TVarRec * Args, const int Args_Size) : \
  88. BASE(Ident, Args, Args_Size) \
  89. { \
  90. } \
  91. inline __fastcall NAME(const UnicodeString Msg, int AHelpContext) : \
  92. BASE(Msg, AHelpContext) \
  93. { \
  94. } \
  95. inline __fastcall NAME(const UnicodeString Msg, const TVarRec * Args, const int Args_Size, int AHelpContext) : \
  96. BASE(Msg, Args, Args_Size, AHelpContext) \
  97. { \
  98. } \
  99. inline __fastcall NAME(NativeUInt Ident, int AHelpContext) : \
  100. BASE(Ident, AHelpContext) \
  101. { \
  102. } \
  103. inline __fastcall NAME(PResStringRec ResStringRec, const TVarRec * Args, const int Args_Size, int AHelpContext) : \
  104. BASE(ResStringRec, Args, Args_Size, AHelpContext) \
  105. { \
  106. } \
  107. virtual ExtException * __fastcall Clone() \
  108. { \
  109. return new NAME(this, L""); \
  110. } \
  111. virtual void __fastcall Rethrow() \
  112. { \
  113. throw NAME(this, L""); \
  114. }
  115. #define DERIVE_EXT_EXCEPTION(NAME, BASE) \
  116. class NAME : public BASE \
  117. { \
  118. EXT_EXCEPTION_METHODS(NAME, BASE) \
  119. };
  120. //---------------------------------------------------------------------------
  121. DERIVE_EXT_EXCEPTION(ESsh, ExtException);
  122. DERIVE_EXT_EXCEPTION(ETerminal, ExtException);
  123. DERIVE_EXT_EXCEPTION(ECommand, ExtException);
  124. DERIVE_EXT_EXCEPTION(EScp, ExtException); // SCP protocol fatal error (non-fatal in application context)
  125. class ESkipFile : public ExtException
  126. {
  127. public:
  128. inline __fastcall ESkipFile() :
  129. ExtException(NULL, UnicodeString())
  130. {
  131. }
  132. EXT_EXCEPTION_METHODS(ESkipFile, ExtException)
  133. };
  134. //---------------------------------------------------------------------------
  135. class EOSExtException : public ExtException
  136. {
  137. public:
  138. __fastcall EOSExtException();
  139. __fastcall EOSExtException(UnicodeString Msg);
  140. __fastcall EOSExtException(UnicodeString Msg, int LastError);
  141. };
  142. //---------------------------------------------------------------------------
  143. class ECRTExtException : public EOSExtException
  144. {
  145. public:
  146. __fastcall ECRTExtException();
  147. __fastcall ECRTExtException(UnicodeString Msg);
  148. };
  149. //---------------------------------------------------------------------------
  150. class EFatal : public ExtException
  151. {
  152. public:
  153. // fatal errors are always copied, new message is only appended
  154. __fastcall EFatal(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = L"");
  155. __property bool ReopenQueried = { read = FReopenQueried, write = FReopenQueried };
  156. virtual ExtException * __fastcall Clone();
  157. virtual void __fastcall Rethrow();
  158. private:
  159. bool FReopenQueried;
  160. };
  161. //---------------------------------------------------------------------------
  162. #define DERIVE_FATAL_EXCEPTION(NAME, BASE) \
  163. class NAME : public BASE \
  164. { \
  165. public: \
  166. inline __fastcall NAME(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword = L"") : \
  167. BASE(E, Msg, HelpKeyword) \
  168. { \
  169. } \
  170. virtual ExtException * __fastcall Clone() \
  171. { \
  172. return new NAME(this, L""); \
  173. } \
  174. };
  175. //---------------------------------------------------------------------------
  176. DERIVE_FATAL_EXCEPTION(ESshFatal, EFatal);
  177. //---------------------------------------------------------------------------
  178. // exception that closes application, but displays info message (not error message)
  179. // = close on completion
  180. class ESshTerminate : public EFatal
  181. {
  182. public:
  183. inline __fastcall ESshTerminate(
  184. Exception* E, UnicodeString Msg, TOnceDoneOperation AOperation,
  185. const UnicodeString & ATargetLocalPath, const UnicodeString & ADestLocalFileName) :
  186. EFatal(E, Msg),
  187. Operation(AOperation),
  188. TargetLocalPath(ATargetLocalPath),
  189. DestLocalFileName(ADestLocalFileName)
  190. {
  191. }
  192. virtual ExtException * __fastcall Clone();
  193. virtual void __fastcall Rethrow();
  194. TOnceDoneOperation Operation;
  195. UnicodeString TargetLocalPath;
  196. UnicodeString DestLocalFileName;
  197. };
  198. //---------------------------------------------------------------------------
  199. class ECallbackGuardAbort : public EAbort
  200. {
  201. public:
  202. __fastcall ECallbackGuardAbort();
  203. };
  204. //---------------------------------------------------------------------------
  205. Exception * __fastcall CloneException(Exception * Exception);
  206. void __fastcall RethrowException(Exception * E);
  207. UnicodeString __fastcall GetExceptionHelpKeyword(Exception * E);
  208. UnicodeString __fastcall MergeHelpKeyword(const UnicodeString & PrimaryHelpKeyword, const UnicodeString & SecondaryHelpKeyword);
  209. bool __fastcall IsInternalErrorHelpKeyword(const UnicodeString & HelpKeyword);
  210. UnicodeString __fastcall AddContextToExceptionMessage(const Exception & E, const UnicodeString & NewContext);
  211. //---------------------------------------------------------------------------
  212. #endif // Exceptions