Exceptions.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Exceptions.h"
  6. #include "TextsCore.h"
  7. #include "HelpCore.h"
  8. #include "Configuration.h"
  9. #include "CoreMain.h"
  10. #include "Interface.h"
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. //---------------------------------------------------------------------------
  14. static bool __fastcall WellKnownException(
  15. Exception * E, UnicodeString * AMessage, const wchar_t ** ACounterName, Exception ** AClone, bool Rethrow)
  16. {
  17. UnicodeString Message;
  18. const wchar_t * CounterName;
  19. std::unique_ptr<Exception> Clone;
  20. bool Result = true;
  21. // EAccessViolation is EExternal
  22. if (dynamic_cast<EAccessViolation*>(E) != NULL)
  23. {
  24. if (Rethrow)
  25. {
  26. throw EAccessViolation(E->Message);
  27. }
  28. Message = MainInstructions(LoadStr(ACCESS_VIOLATION_ERROR3));
  29. CounterName = L"AccessViolations";
  30. Clone.reset(new EAccessViolation(E->Message));
  31. }
  32. // EIntError and EMathError are EExternal
  33. else if ((dynamic_cast<EListError*>(E) != NULL) ||
  34. (dynamic_cast<EStringListError*>(E) != NULL) ||
  35. (dynamic_cast<EIntError*>(E) != NULL) ||
  36. (dynamic_cast<EMathError*>(E) != NULL) ||
  37. (dynamic_cast<EVariantError*>(E) != NULL) ||
  38. (dynamic_cast<EInvalidOperation*>(E) != NULL))
  39. {
  40. if (Rethrow)
  41. {
  42. throw EIntError(E->Message);
  43. }
  44. Message = MainInstructions(E->Message);
  45. CounterName = L"InternalExceptions";
  46. Clone.reset(new EIntError(E->Message));
  47. }
  48. else if (dynamic_cast<EExternal*>(E) != NULL)
  49. {
  50. if (Rethrow)
  51. {
  52. throw EExternal(E->Message);
  53. }
  54. Message = MainInstructions(E->Message);
  55. CounterName = L"ExternalExceptions";
  56. Clone.reset(new EExternal(E->Message));
  57. }
  58. else if (dynamic_cast<EHeapException*>(E) != NULL)
  59. {
  60. if (Rethrow)
  61. {
  62. throw EHeapException(E->Message);
  63. }
  64. Message = MainInstructions(E->Message);
  65. CounterName = L"HeapExceptions";
  66. Clone.reset(new EHeapException(E->Message));
  67. }
  68. else
  69. {
  70. Result = false;
  71. }
  72. if (Result)
  73. {
  74. if (AMessage != NULL)
  75. {
  76. (*AMessage) = Message;
  77. }
  78. if (ACounterName != NULL)
  79. {
  80. (*ACounterName) = CounterName;
  81. }
  82. if (AClone != NULL)
  83. {
  84. (*AClone) = NOT_NULL(Clone.release());
  85. }
  86. }
  87. return Result;
  88. }
  89. //---------------------------------------------------------------------------
  90. static bool __fastcall ExceptionMessage(Exception * E, bool Count,
  91. bool Formatted, UnicodeString & Message, bool & InternalError)
  92. {
  93. bool Result = true;
  94. const wchar_t * CounterName = NULL;
  95. InternalError = false; // see also IsInternalException
  96. // this list has to be in sync with CloneException
  97. if (dynamic_cast<EAbort *>(E) != NULL)
  98. {
  99. Result = false;
  100. }
  101. else if (WellKnownException(E, &Message, &CounterName, NULL, false))
  102. {
  103. InternalError = true;
  104. }
  105. else if (E->Message.IsEmpty())
  106. {
  107. Result = false;
  108. }
  109. else
  110. {
  111. Message = E->Message;
  112. }
  113. if (!Formatted)
  114. {
  115. Message = UnformatMessage(Message);
  116. }
  117. if (InternalError)
  118. {
  119. Message = FMTLOAD(REPORT_ERROR, (Message));
  120. }
  121. if (Count && (CounterName != NULL) && (Configuration->Usage != NULL))
  122. {
  123. Configuration->Usage->Inc(CounterName);
  124. }
  125. return Result;
  126. }
  127. //---------------------------------------------------------------------------
  128. bool __fastcall IsInternalException(Exception * E)
  129. {
  130. // see also InternalError in ExceptionMessage
  131. return WellKnownException(E, NULL, NULL, NULL, false);
  132. }
  133. //---------------------------------------------------------------------------
  134. bool __fastcall ExceptionMessage(Exception * E, UnicodeString & Message)
  135. {
  136. bool InternalError;
  137. return ExceptionMessage(E, true, false, Message, InternalError);
  138. }
  139. //---------------------------------------------------------------------------
  140. bool __fastcall ExceptionMessageFormatted(Exception * E, UnicodeString & Message)
  141. {
  142. bool InternalError;
  143. return ExceptionMessage(E, true, true, Message, InternalError);
  144. }
  145. //---------------------------------------------------------------------------
  146. bool __fastcall ShouldDisplayException(Exception * E)
  147. {
  148. UnicodeString Message;
  149. return ExceptionMessageFormatted(E, Message);
  150. }
  151. //---------------------------------------------------------------------------
  152. TStrings * __fastcall ExceptionToMoreMessages(Exception * E)
  153. {
  154. TStrings * Result = NULL;
  155. UnicodeString Message;
  156. if (ExceptionMessage(E, Message))
  157. {
  158. Result = new TStringList();
  159. Result->Add(Message);
  160. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  161. if ((ExtE != NULL) && (ExtE->MoreMessages != NULL))
  162. {
  163. Result->AddStrings(ExtE->MoreMessages);
  164. }
  165. }
  166. return Result;
  167. }
  168. //---------------------------------------------------------------------------
  169. UnicodeString __fastcall GetExceptionHelpKeyword(Exception * E)
  170. {
  171. UnicodeString HelpKeyword;
  172. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  173. UnicodeString Message; // not used
  174. bool InternalError = false;
  175. if (ExtE != NULL)
  176. {
  177. HelpKeyword = ExtE->HelpKeyword;
  178. }
  179. else if ((E != NULL) && ExceptionMessage(E, false, false, Message, InternalError) &&
  180. InternalError)
  181. {
  182. HelpKeyword = HELP_INTERNAL_ERROR;
  183. }
  184. return HelpKeyword;
  185. }
  186. //---------------------------------------------------------------------------
  187. UnicodeString __fastcall MergeHelpKeyword(const UnicodeString & PrimaryHelpKeyword, const UnicodeString & SecondaryHelpKeyword)
  188. {
  189. if (!PrimaryHelpKeyword.IsEmpty() &&
  190. !IsInternalErrorHelpKeyword(SecondaryHelpKeyword))
  191. {
  192. // we have to yet decide what we have both
  193. // PrimaryHelpKeyword and SecondaryHelpKeyword
  194. return PrimaryHelpKeyword;
  195. }
  196. else
  197. {
  198. return SecondaryHelpKeyword;
  199. }
  200. }
  201. //---------------------------------------------------------------------------
  202. bool __fastcall IsInternalErrorHelpKeyword(const UnicodeString & HelpKeyword)
  203. {
  204. return
  205. (HelpKeyword == HELP_INTERNAL_ERROR);
  206. }
  207. //---------------------------------------------------------------------------
  208. __fastcall ExtException::ExtException(Exception * E) :
  209. Exception("")
  210. {
  211. AddMoreMessages(E);
  212. FHelpKeyword = GetExceptionHelpKeyword(E);
  213. }
  214. //---------------------------------------------------------------------------
  215. __fastcall ExtException::ExtException(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword):
  216. Exception(Msg)
  217. {
  218. AddMoreMessages(E);
  219. FHelpKeyword = MergeHelpKeyword(HelpKeyword, GetExceptionHelpKeyword(E));
  220. }
  221. //---------------------------------------------------------------------------
  222. __fastcall ExtException::ExtException(UnicodeString Msg, Exception* E, UnicodeString HelpKeyword) :
  223. Exception("")
  224. {
  225. // "copy exception"
  226. AddMoreMessages(E);
  227. // and append message to the end to more messages
  228. if (!Msg.IsEmpty())
  229. {
  230. if (Message.IsEmpty())
  231. {
  232. Message = Msg;
  233. }
  234. else
  235. {
  236. if (FMoreMessages == NULL)
  237. {
  238. FMoreMessages = new TStringList();
  239. }
  240. FMoreMessages->Append(UnformatMessage(Msg));
  241. }
  242. }
  243. FHelpKeyword = MergeHelpKeyword(GetExceptionHelpKeyword(E), HelpKeyword);
  244. }
  245. //---------------------------------------------------------------------------
  246. __fastcall ExtException::ExtException(UnicodeString Msg, UnicodeString MoreMessages,
  247. UnicodeString HelpKeyword) :
  248. Exception(Msg),
  249. FHelpKeyword(HelpKeyword)
  250. {
  251. if (!MoreMessages.IsEmpty())
  252. {
  253. FMoreMessages = TextToStringList(MoreMessages);
  254. }
  255. }
  256. //---------------------------------------------------------------------------
  257. __fastcall ExtException::ExtException(UnicodeString Msg, TStrings* MoreMessages,
  258. bool Own, UnicodeString HelpKeyword) :
  259. Exception(Msg),
  260. FHelpKeyword(HelpKeyword)
  261. {
  262. if (Own)
  263. {
  264. FMoreMessages = MoreMessages;
  265. }
  266. else
  267. {
  268. FMoreMessages = new TStringList();
  269. FMoreMessages->Assign(MoreMessages);
  270. }
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall ExtException::AddMoreMessages(Exception* E)
  274. {
  275. if (E != NULL)
  276. {
  277. if (FMoreMessages == NULL)
  278. {
  279. FMoreMessages = new TStringList();
  280. }
  281. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  282. if (ExtE != NULL)
  283. {
  284. if (ExtE->MoreMessages != NULL)
  285. {
  286. FMoreMessages->Assign(ExtE->MoreMessages);
  287. }
  288. }
  289. UnicodeString Msg;
  290. ExceptionMessageFormatted(E, Msg);
  291. // new exception does not have own message, this is in fact duplication of
  292. // the exception data, but the exception class may being changed
  293. if (Message.IsEmpty())
  294. {
  295. Message = Msg;
  296. }
  297. else if (!Msg.IsEmpty())
  298. {
  299. FMoreMessages->Insert(0, UnformatMessage(Msg));
  300. }
  301. if (IsInternalException(E))
  302. {
  303. AppendExceptionStackTraceAndForget(FMoreMessages);
  304. }
  305. if (FMoreMessages->Count == 0)
  306. {
  307. delete FMoreMessages;
  308. FMoreMessages = NULL;
  309. }
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. __fastcall ExtException::~ExtException()
  314. {
  315. delete FMoreMessages;
  316. }
  317. //---------------------------------------------------------------------------
  318. ExtException * __fastcall ExtException::CloneFrom(Exception * E)
  319. {
  320. return new ExtException(E, L"");
  321. }
  322. //---------------------------------------------------------------------------
  323. ExtException * __fastcall ExtException::Clone()
  324. {
  325. return CloneFrom(this);
  326. }
  327. //---------------------------------------------------------------------------
  328. UnicodeString __fastcall SysErrorMessageForError(int LastError)
  329. {
  330. UnicodeString Result;
  331. if (LastError != 0)
  332. {
  333. Result = FORMAT(System_Sysconst_SOSError, (LastError, SysErrorMessage(LastError)));
  334. }
  335. return Result;
  336. }
  337. //---------------------------------------------------------------------------
  338. UnicodeString __fastcall LastSysErrorMessage()
  339. {
  340. return SysErrorMessageForError(GetLastError());
  341. }
  342. //---------------------------------------------------------------------------
  343. __fastcall EOSExtException::EOSExtException(UnicodeString Msg) :
  344. ExtException(Msg, LastSysErrorMessage())
  345. {
  346. }
  347. //---------------------------------------------------------------------------
  348. __fastcall EOSExtException::EOSExtException(UnicodeString Msg, int LastError) :
  349. ExtException(Msg, SysErrorMessageForError(LastError))
  350. {
  351. }
  352. //---------------------------------------------------------------------------
  353. __fastcall EFatal::EFatal(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword) :
  354. ExtException(Msg, E, HelpKeyword),
  355. FReopenQueried(false)
  356. {
  357. EFatal * F = dynamic_cast<EFatal *>(E);
  358. if (F != NULL)
  359. {
  360. FReopenQueried = F->ReopenQueried;
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. ExtException * __fastcall EFatal::Clone()
  365. {
  366. return new EFatal(this, L"");
  367. }
  368. //---------------------------------------------------------------------------
  369. ExtException * __fastcall ESshTerminate::Clone()
  370. {
  371. return new ESshTerminate(this, L"", Operation);
  372. }
  373. //---------------------------------------------------------------------------
  374. __fastcall ECallbackGuardAbort::ECallbackGuardAbort() : EAbort(L"callback abort")
  375. {
  376. }
  377. //---------------------------------------------------------------------------
  378. Exception * __fastcall CloneException(Exception * E)
  379. {
  380. Exception * Result;
  381. // this list has to be in sync with ExceptionMessage
  382. ExtException * Ext = dynamic_cast<ExtException *>(E);
  383. if (Ext != NULL)
  384. {
  385. Result = Ext->Clone();
  386. }
  387. else if (dynamic_cast<ECallbackGuardAbort *>(E) != NULL)
  388. {
  389. Result = new ECallbackGuardAbort();
  390. }
  391. else if (dynamic_cast<EAbort *>(E) != NULL)
  392. {
  393. Result = new EAbort(E->Message);
  394. }
  395. else if (WellKnownException(E, NULL, NULL, &Result, false))
  396. {
  397. // noop
  398. }
  399. else
  400. {
  401. // we do not expect this to happen
  402. if (ALWAYS_FALSE(IsInternalException(E)))
  403. {
  404. // to save exception stack trace
  405. Result = ExtException::CloneFrom(E);
  406. }
  407. else
  408. {
  409. Result = new Exception(E->Message);
  410. }
  411. }
  412. return Result;
  413. }
  414. //---------------------------------------------------------------------------
  415. void __fastcall RethrowException(Exception * E)
  416. {
  417. // this list has to be in sync with ExceptionMessage
  418. if (dynamic_cast<EFatal *>(E) != NULL)
  419. {
  420. throw EFatal(E, L"");
  421. }
  422. else if (dynamic_cast<ECallbackGuardAbort *>(E) != NULL)
  423. {
  424. throw ECallbackGuardAbort();
  425. }
  426. else if (dynamic_cast<EAbort *>(E) != NULL)
  427. {
  428. throw EAbort(E->Message);
  429. }
  430. else if (WellKnownException(E, NULL, NULL, NULL, true))
  431. {
  432. // noop, should never get here
  433. }
  434. else
  435. {
  436. throw ExtException(E, L"");
  437. }
  438. }