Delphi-int.ru: портал программистов

Вход Регистрация | Забыли пароль?

Просмотр кода

Идентификатор: 5981d7d0 Описание: EClasses.pas Код загружен: 1 августа 2011, 16:20 (mirt.steelwater)

  1. unit EClasses;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, SysUtils, TypInfo,
  7. VarRecs;
  8.  
  9. type
  10. {$M+}
  11. EClass = class (Exception)
  12. private
  13. f_EGUID: String; { уникальный идентификатор исключения }
  14. protected
  15. procedure SetEGUID (const aValue: String);
  16. public
  17. constructor Create (anArgs: array of const;
  18. const anEGUID: String = ''); overload;
  19. constructor Create (anArgs: array of const;
  20. anEGUID: array of const); overload;
  21. property EGUID: String read f_EGUID write SetEGUID;
  22. end;
  23. {$M-}
  24.  
  25.  
  26. function toExceptionMessage (const aValue: TVarRec) : String;
  27.  
  28. function RaiseErrorInClass (doRaise: Boolean;
  29. anArgs: array of const;
  30. const anEGUID: String = '') : String;
  31.  
  32. implementation
  33.  
  34. function toExceptionMessage (const aValue: TVarRec) : String;
  35. begin
  36. Result := '';
  37. with aValue do
  38. try
  39. case VType of
  40. vtObject: if VObject.InheritsFrom (Exception) then
  41. Result := Exception (VObject).Message
  42. else
  43. Result := VObject.ClassName;
  44. else Result := toString (aValue);
  45. end;
  46. except
  47. Result := 'Unknown Error';
  48. end;
  49. end;
  50.  
  51. function RaiseErrorInClass (doRaise: Boolean;
  52. anArgs: array of const;
  53. const anEGUID: String = '') : String;
  54. var
  55. I : Integer;
  56.  
  57. { функция определения исполняемого модуля приложения }
  58. function GetClassPackageName (aClass: TClass) : String;
  59. var
  60. M : TMemoryBasicInformation;
  61. begin
  62. { определяем хэндл DLL, которая владеет классом }
  63. VirtualQuery ( aClass, M, SizeOf (M) );
  64. SetLength (Result,MAX_PATH+1);
  65. { если это не главная программа }
  66. if ( hModule (M.AllocationBase) <> hInstance ) then
  67. begin
  68. GetModuleFileName ( hModule (M.AllocationBase), PChar (Result), MAX_PATH );
  69. SetLength ( Result, StrLen ( PChar (Result) ) );
  70. Result := ExtractFileName (Result);
  71. end
  72. else
  73. Result := ExtractFileName ( ParamStr (0) );
  74. end;
  75.  
  76. { функция определения внутреннего модуля }
  77. function GetClassUnitName (aClass: TClass) : String;
  78. var
  79. C : Pointer;
  80. begin
  81. Result := 'Unknown';
  82. C := aClass.ClassInfo;
  83. if Assigned (C) then
  84. Result := GetTypeData (C).UnitName;
  85. end;
  86.  
  87. begin
  88. Result := '';
  89. for I := Low (anArgs) to High (anArgs) do
  90. with anArgs [I] do
  91. begin
  92. { первый параметр - класс, в котором возникло исключение }
  93. if ( I = 0 ) then
  94. begin
  95. case VType of
  96. vtClass: Result := Format( '%s::%s::%s',[ GetClassPackageName (VClass),
  97. GetClassUnitName (VClass),
  98. VClass.ClassName ] );
  99. vtObject: if VObject.InheritsFrom (Exception) then
  100. Result := Exception (VObject).Message
  101. else
  102. Result := Format( '%s::%s::%s',[ GetClassPackageName (VObject.ClassType),
  103. GetClassUnitName (VObject.ClassType),
  104. VObject.ClassName ] );
  105. else Result := toExceptionMessage (anArgs [I]);
  106. end;
  107. end
  108. { второй параметр - имя метода класса, в котором возникло исключение }
  109. else if ( I = 1 ) then
  110. begin
  111. case VType of
  112. vtChar: Result := Format ('%s.%s',[Result,VChar]);
  113. vtString: Result := Format ('%s.%s',[Result,VString^]);
  114. vtPChar: Result := Format ( '%s.%s',[ Result, StrPas (VPChar) ] );
  115. vtAnsiString: Result := Format ( '%s.%s',[ Result, String (VAnsiString) ] );
  116. vtWideChar: Result := Format ( '%s.%s',[ Result, Char (VWideChar) ] );
  117. vtPWideChar: Result := Format ( '%s.%s',[ Result, WideCharToString (VPWideChar) ] );
  118. vtWideString: Result := Format ( '%s.%s',[ Result, WideCharToString (VWideString) ] );
  119. vtVariant: Result := Format ('%s.%s',[Result,VVariant^]);
  120. else Result := Format ( '%s : '#13#10'%s',[ Result, toExceptionMessage (anArgs [I]) ] );
  121. end;
  122. end
  123. { остальные параметры - текстовые сообщения или экземпляры класса исключения }
  124. else
  125. Result := Format ( '%s : '#13#10'%s',[ Result, toExceptionMessage (anArgs [I]) ] );
  126. end;
  127. { уникальный идентификатор исключения }
  128. if ( anEGUID <> '' ) then
  129. Result := Format ('%s'#13#10'%s',[anEGUID,Result]);
  130. if doRaise then
  131. raise Exception.Create (Result);
  132. end;
  133.  
  134. procedure EClass.SetEGUID (const aValue: String);
  135. begin
  136. {$IFDEF HEX_UPPER_CASE}
  137. f_EGUID := UpperCase (aValue);
  138. {$ELSE}
  139. f_EGUID := LowerCase (aValue);
  140. {$ENDIF HEX_UPPER_CASE}
  141. end;
  142.  
  143. constructor EClass.Create (anArgs: array of const;
  144. const anEGUID: String = '');
  145. begin
  146. EGUID := anEGUID;
  147. inherited Create ( RaiseErrorInClass (FALSE,anArgs,anEGUID) );
  148. end;
  149.  
  150. constructor EClass.Create (anArgs: array of const;
  151. anEGUID: array of const);
  152. var
  153. I : Integer;
  154. begin
  155. EGUID := '';
  156. for I := Low (anEGUID) to High (anEGUID) do
  157. EGUID := Format ('%s%s',[ EGUID, toString (anEGUID [I]) ]);
  158. inherited Create ( RaiseErrorInClass (FALSE,anArgs,EGUID) );
  159. end;
  160.  
  161. end.

Ссылка на данный код:

На главную страницу сервиса обмена кодом »