Exceptions.cpp 15 KB

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