1
0

Exceptions.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. UnicodeString __fastcall GetExceptionHelpKeyword(Exception * E)
  204. {
  205. UnicodeString HelpKeyword;
  206. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  207. UnicodeString Message; // not used
  208. bool InternalError = false;
  209. if (ExtE != NULL)
  210. {
  211. HelpKeyword = ExtE->HelpKeyword;
  212. }
  213. else if ((E != NULL) && ExceptionMessage(E, false, false, Message, InternalError) &&
  214. InternalError)
  215. {
  216. HelpKeyword = HELP_INTERNAL_ERROR;
  217. }
  218. return HelpKeyword;
  219. }
  220. //---------------------------------------------------------------------------
  221. UnicodeString __fastcall MergeHelpKeyword(const UnicodeString & PrimaryHelpKeyword, const UnicodeString & SecondaryHelpKeyword)
  222. {
  223. if (!PrimaryHelpKeyword.IsEmpty() &&
  224. !IsInternalErrorHelpKeyword(SecondaryHelpKeyword))
  225. {
  226. // we have to yet decide what we have both
  227. // PrimaryHelpKeyword and SecondaryHelpKeyword
  228. return PrimaryHelpKeyword;
  229. }
  230. else
  231. {
  232. return SecondaryHelpKeyword;
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. bool __fastcall IsInternalErrorHelpKeyword(const UnicodeString & HelpKeyword)
  237. {
  238. return
  239. (HelpKeyword == HELP_INTERNAL_ERROR);
  240. }
  241. //---------------------------------------------------------------------------
  242. __fastcall ExtException::ExtException(Exception * E) :
  243. Exception("")
  244. {
  245. AddMoreMessages(E);
  246. FHelpKeyword = GetExceptionHelpKeyword(E);
  247. }
  248. //---------------------------------------------------------------------------
  249. __fastcall ExtException::ExtException(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword):
  250. Exception(Msg)
  251. {
  252. AddMoreMessages(E);
  253. FHelpKeyword = MergeHelpKeyword(HelpKeyword, GetExceptionHelpKeyword(E));
  254. }
  255. //---------------------------------------------------------------------------
  256. __fastcall ExtException::ExtException(UnicodeString Msg, Exception* E, UnicodeString HelpKeyword) :
  257. Exception("")
  258. {
  259. // "copy exception"
  260. AddMoreMessages(E);
  261. // and append message to the end to more messages
  262. if (!Msg.IsEmpty())
  263. {
  264. if (Message.IsEmpty())
  265. {
  266. Message = Msg;
  267. }
  268. else
  269. {
  270. if (FMoreMessages == NULL)
  271. {
  272. FMoreMessages = new TStringList();
  273. }
  274. FMoreMessages->Append(UnformatMessage(Msg));
  275. }
  276. }
  277. FHelpKeyword = MergeHelpKeyword(GetExceptionHelpKeyword(E), HelpKeyword);
  278. }
  279. //---------------------------------------------------------------------------
  280. __fastcall ExtException::ExtException(UnicodeString Msg, UnicodeString MoreMessages,
  281. UnicodeString HelpKeyword) :
  282. Exception(Msg),
  283. FHelpKeyword(HelpKeyword)
  284. {
  285. if (!MoreMessages.IsEmpty())
  286. {
  287. FMoreMessages = TextToStringList(MoreMessages);
  288. }
  289. }
  290. //---------------------------------------------------------------------------
  291. __fastcall ExtException::ExtException(UnicodeString Msg, TStrings* MoreMessages,
  292. bool Own, UnicodeString HelpKeyword) :
  293. Exception(Msg),
  294. FHelpKeyword(HelpKeyword)
  295. {
  296. if (Own)
  297. {
  298. FMoreMessages = MoreMessages;
  299. }
  300. else
  301. {
  302. FMoreMessages = new TStringList();
  303. FMoreMessages->Assign(MoreMessages);
  304. }
  305. }
  306. //---------------------------------------------------------------------------
  307. void __fastcall ExtException::AddMoreMessages(Exception* E)
  308. {
  309. if (E != NULL)
  310. {
  311. if (FMoreMessages == NULL)
  312. {
  313. FMoreMessages = new TStringList();
  314. }
  315. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  316. if (ExtE != NULL)
  317. {
  318. if (ExtE->MoreMessages != NULL)
  319. {
  320. FMoreMessages->Assign(ExtE->MoreMessages);
  321. }
  322. }
  323. UnicodeString Msg;
  324. ExceptionMessageFormatted(E, Msg);
  325. // new exception does not have own message, this is in fact duplication of
  326. // the exception data, but the exception class may being changed
  327. if (Message.IsEmpty())
  328. {
  329. Message = Msg;
  330. }
  331. else if (!Msg.IsEmpty())
  332. {
  333. FMoreMessages->Insert(0, UnformatMessage(Msg));
  334. }
  335. if (IsInternalException(E))
  336. {
  337. AppendExceptionStackTraceAndForget(FMoreMessages);
  338. }
  339. if (FMoreMessages->Count == 0)
  340. {
  341. delete FMoreMessages;
  342. FMoreMessages = NULL;
  343. }
  344. }
  345. }
  346. //---------------------------------------------------------------------------
  347. __fastcall ExtException::~ExtException()
  348. {
  349. delete FMoreMessages;
  350. }
  351. //---------------------------------------------------------------------------
  352. ExtException * __fastcall ExtException::CloneFrom(Exception * E)
  353. {
  354. return new ExtException(E, L"");
  355. }
  356. //---------------------------------------------------------------------------
  357. ExtException * __fastcall ExtException::Clone()
  358. {
  359. return CloneFrom(this);
  360. }
  361. //---------------------------------------------------------------------------
  362. UnicodeString __fastcall SysErrorMessageForError(int LastError)
  363. {
  364. UnicodeString Result;
  365. if (LastError != 0)
  366. {
  367. Result = FORMAT(System_Sysconst_SOSError, (LastError, SysErrorMessage(LastError), UnicodeString()));
  368. }
  369. return Result;
  370. }
  371. //---------------------------------------------------------------------------
  372. UnicodeString __fastcall LastSysErrorMessage()
  373. {
  374. return SysErrorMessageForError(GetLastError());
  375. }
  376. //---------------------------------------------------------------------------
  377. __fastcall EOSExtException::EOSExtException(UnicodeString Msg) :
  378. ExtException(Msg, LastSysErrorMessage())
  379. {
  380. }
  381. //---------------------------------------------------------------------------
  382. __fastcall EOSExtException::EOSExtException(UnicodeString Msg, int LastError) :
  383. ExtException(Msg, SysErrorMessageForError(LastError))
  384. {
  385. }
  386. //---------------------------------------------------------------------------
  387. __fastcall EFatal::EFatal(Exception* E, UnicodeString Msg, UnicodeString HelpKeyword) :
  388. ExtException(Msg, E, HelpKeyword),
  389. FReopenQueried(false)
  390. {
  391. EFatal * F = dynamic_cast<EFatal *>(E);
  392. if (F != NULL)
  393. {
  394. FReopenQueried = F->ReopenQueried;
  395. }
  396. }
  397. //---------------------------------------------------------------------------
  398. __fastcall ECRTExtException::ECRTExtException(UnicodeString Msg) :
  399. EOSExtException(Msg, errno)
  400. {
  401. }
  402. //---------------------------------------------------------------------------
  403. ExtException * __fastcall EFatal::Clone()
  404. {
  405. return new EFatal(this, L"");
  406. }
  407. //---------------------------------------------------------------------------
  408. ExtException * __fastcall ESshTerminate::Clone()
  409. {
  410. return new ESshTerminate(this, L"", Operation);
  411. }
  412. //---------------------------------------------------------------------------
  413. __fastcall ECallbackGuardAbort::ECallbackGuardAbort() : EAbort(L"callback abort")
  414. {
  415. }
  416. //---------------------------------------------------------------------------
  417. Exception * __fastcall CloneException(Exception * E)
  418. {
  419. Exception * Result;
  420. // this list has to be in sync with ExceptionMessage
  421. ExtException * Ext = dynamic_cast<ExtException *>(E);
  422. if (Ext != NULL)
  423. {
  424. Result = Ext->Clone();
  425. }
  426. else if (dynamic_cast<ECallbackGuardAbort *>(E) != NULL)
  427. {
  428. Result = new ECallbackGuardAbort();
  429. }
  430. else if (dynamic_cast<EAbort *>(E) != NULL)
  431. {
  432. Result = new EAbort(E->Message);
  433. }
  434. else if (WellKnownException(E, NULL, NULL, &Result, false))
  435. {
  436. // noop
  437. }
  438. else
  439. {
  440. // we do not expect this to happen
  441. if (DebugAlwaysFalse(IsInternalException(E)))
  442. {
  443. // to save exception stack trace
  444. Result = ExtException::CloneFrom(E);
  445. }
  446. else
  447. {
  448. Result = new Exception(E->Message);
  449. }
  450. }
  451. return Result;
  452. }
  453. //---------------------------------------------------------------------------
  454. void __fastcall RethrowException(Exception * E)
  455. {
  456. // this list has to be in sync with ExceptionMessage
  457. if (dynamic_cast<EFatal *>(E) != NULL)
  458. {
  459. throw EFatal(E, L"");
  460. }
  461. else if (dynamic_cast<ECallbackGuardAbort *>(E) != NULL)
  462. {
  463. throw ECallbackGuardAbort();
  464. }
  465. else if (dynamic_cast<EAbort *>(E) != NULL)
  466. {
  467. throw EAbort(E->Message);
  468. }
  469. else if (WellKnownException(E, NULL, NULL, NULL, true))
  470. {
  471. // noop, should never get here
  472. }
  473. else
  474. {
  475. throw ExtException(E, L"");
  476. }
  477. }