1
0

Exceptions.cpp 13 KB

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