I was sure this used to work for me, and I’ve seen it out on the net (Jolyon Smith and David Moorhouse). Having just tried it in a simple program both in D2007 and in XE2 trial, it doesn’t keep the modified Message. As soon as the “raise” happens, the message reverts back to the original exception.
What blindlingly obvious thing am I missing ? The alternative is to “raise Exception.Create(…)” but I want to just propogate the original exception back up the chain, only with additional information tagged along at each exception block.
var a: Integer;
begin
try
a := 0;
Label1.Caption := IntToStr(100 div a);
except
on e: Exception do
begin
e.Message := 'Extra Info Plus the original : ' + e.Message;
raise;
end;
end;
end;
Well blow me! This looked so wrong that I had to try it myself, and you’re absolutely right! I’ve narrowed it down to the fact that this is an OS exception (divide by zero) that is generated by the OS itself and not by Delphi. If you try and raise an EIntError yourself you get the expected behaviour, and not what you see above. Note that expected behaviour occurs whenever you raise an exception yourself.
Update: In the System.pas unit there is the following code called when the exception is re-raised:
So if the exception is not a Delphi exception (in this case an OS exception) then the (modified) “Delphi” exception is freed and the original exception is re-raised, thereby throwing away any changes made to the exception. Case closed!
Update 2: (couldn’t help myself). You can reproduce this with the following code:
Have fun!