Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-07

Strange syntax in raising exceptions

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

While porting DUnit2 to Free Pascal I came across the following code. I have never seen syntax like that, yet it is accepted by FPC and Delphi compilers.

Is this documented in FPC docs? What does the '... at <some_function_returning_a_pointer>' do?

Unfortunately I don't have any docs (fpc, delphi or kylix) available here on my home pc, but I'll try and look for tips somebody else could give some insight it would be much appreciated.

=======================================
procedure TTestProc.Warn(const ErrorMsg: string; const ErrorAddress: Pointer);
begin
  if ErrorAddress = nil then
    raise EPostTestFailure.Create(ErrorMsg) at CallerAddr
  else
    raise EPostTestFailure.Create(ErrorMsg) at ErrorAddress;
end;

=======================================

Regards,
- Graeme -

________________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Paul Ishenin
to Graeme Geldenhuys
Graeme Geldenhuys wrote 22 lines
Hi, While porting DUnit2 to Free Pascal I came across the following code. I have never seen syntax like that, yet it is accepted by FPC and Delphi compilers. Is this documented in FPC docs? What does the '... at <some_function_returning_a_pointer>' do? Unfortunately I don't have any docs (fpc, delphi or kylix) available here on my home pc, but I'll try and look for tips tomorrow at work. In somebody else could give some insight it would be much appreciated. ======================================= procedure TTestProc.Warn(const ErrorMsg: string; const ErrorAddress: Pointer); begin if ErrorAddress = nil then raise EPostTestFailure.Create(ErrorMsg) at CallerAddr else raise EPostTestFailure.Create(ErrorMsg) at ErrorAddress; end;

Usually you place raise ESomeException.Create() to paticular place where it happen. Therefore in IDE or in your application you will see error message with address of function where exception was raised.

But if you place raise ESomeException.Create() to some special function which only raises it like your Warn fiunction then address of exception will be useless. Who wants to know that it was raised in Warn function? Better to assign a caller address to that exception. For this we have 'at' keyword.

You can replace this code:
if ErrorAddress = nil then
raise EPostTestFailure.Create(ErrorMsg) at CallerAddr

by next magic functions in fpc:
if ErrorAddress = nil then
raise EPostTestFailure.Create(ErrorMsg) at get_caller_addr(get_frame)

Best regards,
Paul Ishenin.

Graeme Geldenhuys
to Paul Ishenin
Paul Ishenin wrote
will be useless. Who wants to know that it was raised in Warn function? Better to assign a caller address to that exception. For this we have 'at' keyword.

Thanks Paul! Your explanation makes perfect sense. Amazingly enough, I have been using exceptions for many years, and never knew about the 'at' keyword for them. So we learn. :)

PS:
I can also confirm that the 'at <address>' is documented in the Kylix 3 Help and the FPC Reference Guide.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/