Exceptions.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Exceptions.h"
  6. #include "TextsCore.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. //---------------------------------------------------------------------------
  10. __fastcall ExtException::ExtException(Exception * E) :
  11. Exception("")
  12. {
  13. AddMoreMessages(E);
  14. }
  15. //---------------------------------------------------------------------------
  16. __fastcall ExtException::ExtException(Exception* E, AnsiString Msg):
  17. Exception(Msg)
  18. {
  19. AddMoreMessages(E);
  20. }
  21. //---------------------------------------------------------------------------
  22. __fastcall ExtException::ExtException(AnsiString Msg, Exception* E) :
  23. Exception("")
  24. {
  25. // "copy exception"
  26. AddMoreMessages(E);
  27. // and append message to the end to more messages
  28. if (!Msg.IsEmpty())
  29. {
  30. if (Message.IsEmpty())
  31. {
  32. Message = Msg;
  33. }
  34. else
  35. {
  36. if (FMoreMessages == NULL)
  37. {
  38. FMoreMessages = new TStringList();
  39. }
  40. FMoreMessages->Append(Msg);
  41. }
  42. }
  43. }
  44. //---------------------------------------------------------------------------
  45. __fastcall ExtException::ExtException(Exception* E, int Ident):
  46. Exception(Ident)
  47. {
  48. AddMoreMessages(E);
  49. }
  50. //---------------------------------------------------------------------------
  51. __fastcall ExtException::ExtException(AnsiString Msg, AnsiString MoreMessages) :
  52. Exception(Msg)
  53. {
  54. if (!MoreMessages.IsEmpty())
  55. {
  56. FMoreMessages = new TStringList();
  57. FMoreMessages->Text = MoreMessages;
  58. }
  59. }
  60. //---------------------------------------------------------------------------
  61. __fastcall ExtException::ExtException(AnsiString Msg, TStrings* MoreMessages,
  62. bool Own) :
  63. Exception(Msg)
  64. {
  65. if (Own)
  66. {
  67. FMoreMessages = MoreMessages;
  68. }
  69. else
  70. {
  71. FMoreMessages = new TStringList();
  72. FMoreMessages->Assign(MoreMessages);
  73. }
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall ExtException::AddMoreMessages(Exception* E)
  77. {
  78. if (E != NULL)
  79. {
  80. if (FMoreMessages == NULL)
  81. {
  82. FMoreMessages = new TStringList();
  83. }
  84. ExtException * ExtE = dynamic_cast<ExtException *>(E);
  85. if ((ExtE != NULL) &&
  86. (ExtE->MoreMessages != NULL))
  87. {
  88. FMoreMessages->Assign(ExtE->MoreMessages);
  89. }
  90. AnsiString Msg;
  91. if (dynamic_cast<EAccessViolation*>(E) != NULL)
  92. {
  93. Msg = LoadStr(ACCESS_VIOLATION_ERROR);
  94. }
  95. else if (!E->Message.IsEmpty() && (dynamic_cast<EAbort *>(E) == NULL))
  96. {
  97. Msg = E->Message;
  98. }
  99. // new exception does not have own message, this is in fact duplication of
  100. // the exception data, but the exception class may being changed
  101. if (Message.IsEmpty())
  102. {
  103. Message = Msg;
  104. }
  105. else if (!Msg.IsEmpty())
  106. {
  107. FMoreMessages->Insert(0, Msg);
  108. }
  109. if (FMoreMessages->Count == 0)
  110. {
  111. delete FMoreMessages;
  112. FMoreMessages = NULL;
  113. }
  114. }
  115. }
  116. //---------------------------------------------------------------------------
  117. __fastcall ExtException::~ExtException()
  118. {
  119. delete FMoreMessages;
  120. }
  121. //---------------------------------------------------------------------------
  122. AnsiString __fastcall LastSysErrorMessage()
  123. {
  124. int LastError = GetLastError();
  125. AnsiString Result;
  126. if (LastError != 0)
  127. {
  128. Result = FORMAT(Sysconst_SOSError, (LastError, SysErrorMessage(LastError)));
  129. }
  130. return Result;
  131. }
  132. //---------------------------------------------------------------------------
  133. __fastcall EOSExtException::EOSExtException(AnsiString Msg) :
  134. ExtException(Msg, LastSysErrorMessage())
  135. {
  136. }