Mislim da ne mozes tacno dobiti podatak u kojoj liniji koda je doslo do greske, osim ako ne koristis neki debugger i uradis build sa debug informations.
Ali mozes koristiti Assert tako sto ces postaviti flag [{$ASSERTIONS ON}] u src projekta i onda kada god uradis npr.
Code:
try
...
except
Assert(False, 'npr. <record update failed!>')
end;
dobices exception #227 ako se pozove Assert.
Moze se koristiti i npr.
Code:
Assert(CopyFile(...), 'CopyFile failed');
//Kopiranje ce se izvrsiti ako je sve ok
//ako nije, tj CopyFile vrati false, izvrsice se assert
Assert(Now < TimeOut, 'Timeout error')
///itd.
Exception #227 ces overridovati tako sto ces dodeliti ugradjeni procedure pointer AssertErrorProc na neku svoju proceduru koja je u obliku
Code:
procedure MyEAssertProc(const Message, Filename: string; LineNumber: Integer; ErrorAddr: Pointer);
Znaci. napises nesto ovako u glavnom fajlu projekta (dpr).
Code:
program ...
uses
SysUtils,
...
{$ASSERTIONS ON}
procedure MyEAssertHandler(const Message, Filename: string; LineNumber: Integer; ErrorAddr: Pointer);
const
DebugFileName = 'C:\Debug.txt';
var
DebugFile: TextFile;
ErrorAddress: String;
begin
AssignFile(DebugFile, DebugFileName);
if FileExists(DebugFileName) then
Append(DebugFile)
else
ReWrite(DebugFile);
ErrorAddress := IntToHex(Integer(ErrorAddr), 0);
WriteLn(DebugFile, '***' + FormatDateTime('dd.MM.yyyy hh:nn', Now) + ' Error at adress ' + ErrorAddress + ' near line #' + IntToStr(LineNumber) + ' in ' + ExtractFileName(FileName) + ': ' + Message);
CloseFile(DebugFile);
end;
begin
Application.Initialize;
AssertErrorProc := @MyEAssertHandler;
...
end.
Samo da znas, Line# je broj linije gde je pozvan Assert a ne gde je zaista doslo do greske, ali i to je najcesce sasvim dovoljno da resis problem.
Jos jedna dobra strana ovog metoda je i to sto mozes da logujes svoj tip gresaka ili upozorenja, npr. Assert(False, 'User canceled, exiting silently').