Advertisement
RandomClear

How to ignore particular exception

Jun 11th, 2015
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.26 KB | None | 0 0
  1. // See also: http://pastebin.com/XWpyDuw6 - "how to silently process exception"
  2. // See also: http://pastebin.com/ACtr0r6S - "how to silently log exception"
  3. // See also: http://pastebin.com/QiicyV2E - "how to log all (handled) exceptions"
  4.  
  5. // Option 1: use custom attributes
  6. // Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_custom_attributes.php
  7.  
  8. // Option 1.A:
  9.  
  10. uses
  11.   EClasses,
  12.   ETypes;
  13.  
  14. type
  15.   [EurekaLogHandler(fhtRTL)]
  16.   ETestException = class(Exception);
  17.  
  18. procedure TForm1.Button1Click(Sender: TObject);
  19. begin
  20.   // ETestException will be ignored by EurekaLog and
  21.   // it always will be handled by your application
  22.   // (as if EurekaLog would be disabled).
  23.   // In other words, this will be catched by standard
  24.   // Application.HandleException method.
  25.   // A default MessageBox will be shown.
  26.   raise ETestException.Create('Error Message');
  27. end;
  28.  
  29. // Option 1.B:
  30.  
  31. uses
  32.   EClasses;
  33.  
  34. type
  35.   [EurekaLogExpected()]
  36.   ETestException = class(Exception);
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. begin
  40.   // ETestException will be handled by EurekaLog.
  41.   // However, it would be considered as expected.
  42.   // In other words, this will be catched and shown by EurekaLog.
  43.   // But (EurekaLog) error dialog will lack
  44.   // "details", "send" and "restart" options.
  45.   raise ETestException.Create('Error Message');
  46. end;
  47.  
  48. // Option 2: use filters
  49. // Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_filters.php
  50.  
  51. // Usually you add exception filters at design-time here: http://www.eurekalog.com/help/eurekalog/exceptions_filters_page.php
  52. // However, you can also create filter manually:
  53.  
  54. // Option 2.A:
  55.  
  56. uses
  57.   EModules;
  58.  
  59.   CurrentEurekaLogOptions.ExceptionsFilters.AddIgnoredException(ETestException);
  60.  
  61.   // ETestException will be ignored by EurekaLog and
  62.   // it always will be handled by your application
  63.   // (as if EurekaLog would be disabled).
  64.   raise ETestException.Create('Error Message');
  65.  
  66. // Option 2.B:
  67.  
  68. uses
  69.   EModules;
  70.  
  71.   CurrentEurekaLogOptions.ExceptionsFilters.AddExpectedException(ETestException);
  72.  
  73.   // ETestException will be handled by EurekaLog.
  74.   // However, it would be considered as expected.
  75.   // "details", "send" and "restart" options will not be shown.
  76.   raise ETestException.Create('Error Message');
  77.  
  78. // Option 2.C:
  79.  
  80. uses
  81.   EModules;
  82.  
  83.   CurrentEurekaLogOptions.ExceptionsFilters.AddFullyIgnoredException(ETestException);
  84.  
  85.   // ETestException will be handled by EurekaLog.
  86.   // However, it would be considered as ignored.
  87.   // No dialog, no bug report, no sending.
  88.   // Exception will act like EAbort.
  89.   raise ETestException.Create('Error Message');
  90.  
  91. // Option 2.D:
  92.  
  93. uses
  94.   EModules,
  95.   ETypes;
  96.  
  97. // Create custom filter:
  98. var
  99.   Filter: TEurekaExceptionFilter;
  100. begin
  101.   FillChar(Filter, SizeOf(Filter), 0);
  102.  
  103.   Filter.Active := True;
  104.   Filter.ExceptionClassName := ETestException.ClassName;
  105.   Filter.ExceptionType := fetAll;
  106.   Filter.DialogType := edtUnchanged;
  107.   Filter.HandlerType := fhtRTL;
  108.   Filter.ActionType := fatUnchanged;
  109.  
  110.   CurrentEurekaLogOptions.ExceptionsFilters.Add(Filter);
  111.  
  112.   // ETestException will be ignored by EurekaLog and
  113.   // it always will be handled by your application
  114.   // (as if EurekaLog would be disabled).
  115.   raise ETestException.Create('Error Message');
  116. end;
  117.  
  118. // Option 3: use event handlers
  119. // Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_events.php
  120.  
  121. // Option 3.A:
  122.  
  123. uses
  124.   EEvents,
  125.   EException,
  126.   ETypes;
  127.  
  128. type
  129.   ETestException = class(Exception);
  130.  
  131. procedure TForm1.Button1Click(Sender: TObject);
  132. begin
  133.   // ETestException will be ignored by EurekaLog and
  134.   // it always will be handled by your application
  135.   // (as if EurekaLog would be disabled).
  136.   raise ETestException.Create('Error Message');
  137. end;
  138.  
  139. procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
  140. begin
  141.   // Specify EurekaLog to ignore ETestException
  142.   if AExceptionInfo.ExceptionClass = ETestException.ClassName then // <- this check matches ETestException only, but skips child classes
  143.   begin
  144.     AHandle := False;
  145.     ACallNextHandler := False;
  146.     Exit;
  147.   end;
  148. end;
  149.  
  150. initialization
  151.   RegisterEventExceptionNotify(nil, ExceptionNotify);
  152. end.
  153.  
  154. // Option 3.B:
  155.  
  156. uses
  157.   EEvents,
  158.   EException,
  159.   ETypes;
  160.  
  161. type
  162.   ETestException = class(Exception);
  163.  
  164. procedure TForm1.Button1Click(Sender: TObject);
  165. begin
  166.   // ETestException will be handled by EurekaLog.
  167.   // However, it would be considered as expected.
  168.   // "details", "send" and "restart" options will not be shown.
  169.   raise ETestException.Create('Error Message');
  170. end;
  171.  
  172. procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
  173. begin
  174.   // Mark exception as expected, but allow it to be handled by EurekaLog.
  175.   if (AExceptionInfo.ExceptionObject <> nil) and
  176.      (AExceptionInfo.ExceptionNative) and
  177.      (Exception(AExceptionInfo.ExceptionObject).InheritsFrom(ETestException)) and // <- this check matches ETestException and any of its child classes
  178.      (AExceptionInfo.ExceptionMessage = 'Error Message') then
  179.   begin
  180.     AExceptionInfo.ExpectedContext := -1;
  181.     Exit;
  182.   end;
  183. end;
  184.  
  185. initialization
  186.   RegisterEventExceptionNotify(nil, ExceptionNotify);
  187. end.
  188.  
  189. // Additional note:
  190. // Here is a way to check additional properties of your exception:
  191. procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
  192. begin
  193.   if (AExceptionInfo.ExceptionObject <> nil) and // check that there is exception object
  194.      (AExceptionInfo.ExceptionNative) and // check that it is a Delphi/Builder object
  195.      (Exception(AExceptionInfo.ExceptionObject).InheritsFrom(EOSError)) and // check exception class
  196.      (EOSError(AExceptionInfo.ExceptionObject).ErrorCode = ERROR_ACCESS_DENIED) and // check any additional exception properties
  197.      (AExceptionInfo.CallStack.Count > 0) and
  198.      (AExceptionInfo.CallStack[0].Location.UnitName = 'ServiceProvider') and
  199.      (AExceptionInfo.CallStack[0].Location.ProcedureName = 'Connect') then
  200.   begin
  201.     AExceptionInfo.ExpectedContext := -1;
  202.     Exit;
  203.   end;
  204. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement