Technically, what you have will work just fine, as the caught Exception is just another object in memory, so you can freely modify it before re-raising it.
However, it is not "best practice" to modify a caught exception. A better option would be to raise a new Exception object that has your desired details in it, and the caught Exception is assigned to the new exception's InnerException property. Use the Exception.RaiseOuterException() method for this purpose, eg:
type
ECantOpenFileException = class(Exception)
public
FileName: string;
constructor Create(const AFileName: string);
end;
constructor ECantOpenFileException.Create(const AFileName: string);
begin
inherited Create('File "' + AFileName + '" could not be opened');
FileName := AFileName;
end;
...
try
// ...
raise EFileNotFoundException.Create('File not found!');
except
on E: Exception do begin
Exception.RaiseOuterException(ECantOpenFileException.Create('nonexistent.txt'));
end;
end;
If a higher exception handler wants to know why you raised an exception, they can walk the exception's InnerException chain to gather those details, eg:
try
...
except
on E: Exception do
begin
var Msg := E.Message;
var innerEx := E.InnerException;
while innerEx <> nil do begin
Msg := Msg + #10' ' + innerEx.Message;
innerEx := innerEx.InnerException;
end;
ShowMessage(Msg);
end;
end;