Generic exception handler in service

Hi,

I use MadExcept for exception handling and am wandering how I can use that inside my implementation unit without having to code it in every procedure.

Thanks,

Hi,

You can create invoker’s descendant and put exception handling to it.

type
  TMyNewService_Invoker = class(TNewService_Invoker)
  protected
    procedure AfterInvoke(aMethodPtr: TMessageInvokeMethod;
      const anInstance: IInterface;
      const aFactory: IROClassFactory;
      const aMessage: IROMessage;
      const aTransport: IROTransport;
      anException: Exception); override; //<< check anException here
  end;
...

initialization
  fClassFactory_NewService := TROClassFactory.Create(__ServiceName, {$IFDEF FPC}@{$ENDIF}Create_NewService, TMyNewService_Invoker); //<<TNewService_Invoker replaced with TMyNewService_Invoker

check also TROInvoker.CustomHandleMessage implementation for understanding when AfterInvoke is called.

Thanks, this works perfect. I’m right to assume that just checking Assigned(anException) is the way to check an exception exists?

yes, you are right.

...
        AfterInvoke(mtd, instance, aFactory, aMessage, aTransport, nil);
      except
        on E: Exception do begin
          AfterInvoke(mtd, instance, aFactory, aMessage, aTransport, E); //<< you should catch this call only

Thanks, Evgeny!