RandomClear icon

How to ignore particular exception

RandomClear | PRO | 06/11/15 09:00:52 PM UTC | 0 ⭐ | 9723 👁️ | Never ⏰ | []
Delphi |

6.26 KB

|

None

|

0 👍

/

0 👎

// See also: http://pastebin.com/XWpyDuw6 - "how to silently process exception"
// See also: http://pastebin.com/ACtr0r6S - "how to silently log exception"
// See also: http://pastebin.com/QiicyV2E - "how to log all (handled) exceptions"
 
// Option 1: use custom attributes
// Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_custom_attributes.php
 
// Option 1.A:
 
uses
  EClasses,
  ETypes;
 
type
  [EurekaLogHandler(fhtRTL)]
  ETestException = class(Exception);
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  // ETestException will be ignored by EurekaLog and
  // it always will be handled by your application
  // (as if EurekaLog would be disabled).
  // In other words, this will be catched by standard
  // Application.HandleException method.
  // A default MessageBox will be shown.
  raise ETestException.Create('Error Message');
end;
 
// Option 1.B:
 
uses
  EClasses;
 
type
  [EurekaLogExpected()]
  ETestException = class(Exception);
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  // ETestException will be handled by EurekaLog.
  // However, it would be considered as expected.
  // In other words, this will be catched and shown by EurekaLog.
  // But (EurekaLog) error dialog will lack 
  // "details", "send" and "restart" options.
  raise ETestException.Create('Error Message');
end;
 
// Option 2: use filters
// Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_filters.php
 
// Usually you add exception filters at design-time here: http://www.eurekalog.com/help/eurekalog/exceptions_filters_page.php
// However, you can also create filter manually:
 
// Option 2.A:
 
uses
  EModules;
 
  CurrentEurekaLogOptions.ExceptionsFilters.AddIgnoredException(ETestException);
 
  // ETestException will be ignored by EurekaLog and
  // it always will be handled by your application
  // (as if EurekaLog would be disabled).
  raise ETestException.Create('Error Message');
 
// Option 2.B:
 
uses
  EModules;
 
  CurrentEurekaLogOptions.ExceptionsFilters.AddExpectedException(ETestException);
 
  // ETestException will be handled by EurekaLog.
  // However, it would be considered as expected.
  // "details", "send" and "restart" options will not be shown.
  raise ETestException.Create('Error Message');
 
// Option 2.C:
 
uses
  EModules;
 
  CurrentEurekaLogOptions.ExceptionsFilters.AddFullyIgnoredException(ETestException);
 
  // ETestException will be handled by EurekaLog.
  // However, it would be considered as ignored.
  // No dialog, no bug report, no sending. 
  // Exception will act like EAbort.
  raise ETestException.Create('Error Message');
 
// Option 2.D:
 
uses
  EModules,
  ETypes;
 
// Create custom filter:
var
  Filter: TEurekaExceptionFilter;
begin
  FillChar(Filter, SizeOf(Filter), 0);
 
  Filter.Active := True;
  Filter.ExceptionClassName := ETestException.ClassName;
  Filter.ExceptionType := fetAll;
  Filter.DialogType := edtUnchanged;
  Filter.HandlerType := fhtRTL;
  Filter.ActionType := fatUnchanged;
 
  CurrentEurekaLogOptions.ExceptionsFilters.Add(Filter);
 
  // ETestException will be ignored by EurekaLog and
  // it always will be handled by your application
  // (as if EurekaLog would be disabled).
  raise ETestException.Create('Error Message');
end;
 
// Option 3: use event handlers
// Read more about this method: http://www.eurekalog.com/help/eurekalog/customizing_events.php
 
// Option 3.A:
 
uses
  EEvents,
  EException,
  ETypes;
 
type
  ETestException = class(Exception);
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  // ETestException will be ignored by EurekaLog and
  // it always will be handled by your application
  // (as if EurekaLog would be disabled).
  raise ETestException.Create('Error Message');
end;
 
procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
begin
  // Specify EurekaLog to ignore ETestException
  if AExceptionInfo.ExceptionClass = ETestException.ClassName then // <- this check matches ETestException only, but skips child classes
  begin
    AHandle := False;
    ACallNextHandler := False;
    Exit;
  end;
end;
 
initialization
  RegisterEventExceptionNotify(nil, ExceptionNotify);
end.
 
// Option 3.B:
 
uses
  EEvents,
  EException,
  ETypes;
 
type
  ETestException = class(Exception);
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  // ETestException will be handled by EurekaLog.
  // However, it would be considered as expected.
  // "details", "send" and "restart" options will not be shown.
  raise ETestException.Create('Error Message');
end;
 
procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
begin
  // Mark exception as expected, but allow it to be handled by EurekaLog.
  if (AExceptionInfo.ExceptionObject <> nil) and
     (AExceptionInfo.ExceptionNative) and
     (Exception(AExceptionInfo.ExceptionObject).InheritsFrom(ETestException)) and // <- this check matches ETestException and any of its child classes
     (AExceptionInfo.ExceptionMessage = 'Error Message') then
  begin
    AExceptionInfo.ExpectedContext := -1;
    Exit;
  end;
end;
 
initialization
  RegisterEventExceptionNotify(nil, ExceptionNotify);
end.
 
// Additional note:
// Here is a way to check additional properties of your exception:
procedure ExceptionNotify(const ACustom: Pointer; AExceptionInfo: TEurekaExceptionInfo; var AHandle: Boolean; var ACallNextHandler: Boolean);
begin
  if (AExceptionInfo.ExceptionObject <> nil) and // check that there is exception object
     (AExceptionInfo.ExceptionNative) and // check that it is a Delphi/Builder object
     (Exception(AExceptionInfo.ExceptionObject).InheritsFrom(EOSError)) and // check exception class
     (EOSError(AExceptionInfo.ExceptionObject).ErrorCode = ERROR_ACCESS_DENIED) and // check any additional exception properties
     (AExceptionInfo.CallStack.Count > 0) and
     (AExceptionInfo.CallStack[0].Location.UnitName = 'ServiceProvider') and
     (AExceptionInfo.CallStack[0].Location.ProcedureName = 'Connect') then
  begin
    AExceptionInfo.ExpectedContext := -1;
    Exit;
  end;
end;

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎