Question

This is sample of re-raise exception and working well

  try
    raise Exception.Create('Exception msg');
  except
    on E: Exception do
    begin
      if not(e is EConvertError) then
        raise; // re-raise exception
    end;
  end;

and here is my Custemize method

//    uses fib //fibplus  EFIBInterBaseError
    procedure CustomizeException(e: Exception);
    var
      s: String;
    begin
      if E is EFIBInterBaseError then
      begin
        if Pos('unique',e.Message)>0 then
        begin
          s := 'record';
          if Pos('CUSTOMMERS_IDX1',e.Message)>0 then
            s:= 'Custommer Number';

          raise TCustomizedException.CreateFmt('The %s is already exists.',[s]);
        end
        else
        if Pos('</CMSG>',e.Message)>0 then
        Begin
          raise TCustomizedException.CreateFmt('%s',
          [StrUtilsEx.GiveInTags(e.Message,'<CMSG>','</CMSG>')]
          );
        End
        else
          raise EFIBInterBaseError.CreateFmt('%s',[e.Message]);
      end
      else
        raise Exception.Create(e.Message);   //e;// e.Create(e.Message);
    end;

But

  try
    raise EConvertError.Create('Error Message');
  except on e : exception do
    Begin
      ShowMessage(Format('%s -> %s',[e.ClassName , e.Message])); //(1)
      try
        CustomizeException(e);
      except on e2: Exception do
        ShowMessage(Format('%s -> %s',[e2.ClassName , e2.Message])); //(2)
      end;
    End;
  end;

Result

(1)->EConvertError -> Error Message

(2)->Exception -> Error Message

when i change last line like this code is working well

  else
    raise e;

(1)->EConvertError -> Error Message

(2)->EConvertError -> Error Message

but i'm getting "Access violation at address 00405F04 in module 'Test.exe'. Read of address 00000000." after messages

How to raise same exception type as base exception

The Solution is raise TObject(AcquireExceptionObject); //<- I would like to solve with "E :

type
  ECustomizedException = class(Exception);
 uses 
  fib,SysUtils,System


class procedure SystemEx.CustomizeException(E : Exception);
var
  s: String;
begin
  if E is EFIBInterBaseError then
  begin
    if Pos('unique',e.Message)>0 then
    begin
      s := 'Record';
      if Pos('CUSTOMMER_IDX1',e.Message)>0 then
        s:= 'Custommer';

      raise ECustomizedException.CreateFmt('%s is already exists.',[s]);
    end
    else
    if Pos('</CMSG>',e.Message)>0 then
    Begin
      raise ECustomizedException.CreateFmt('%s',
      [StrUtilsEx.GiveInTags(e.Message,'<CMSG>','</CMSG>')]
      );
    End
    else
      raise EFIBInterBaseError.CreateFmt('%s',[e.Message]);
  end
  else
    raise TObject(AcquireExceptionObject); //<- I would like to solve with "E : Exception" param
//    raise Exception.Create(e.Message);   //e;// e.Create(e.Message);// Exception.Create(E.Message);
End.
Was it helpful?

Solution

The problem you face is that if an exception is caught in an except block the "end" will free the exception instance that you just raised again. So the next except block will catch the already released Exception instance. But you can prevent this by calling AcquireExceptionObject which makes you the owner of the exception instance.

Because you "can't" use raise; (System.@RaiseAgain) you can throw the same exception instance with raise AcquireExceptionObject;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top