E2093 Label '%s' is not declared in current procedure (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

In contrast to standard Pascal, Borland's Object Pascal language does not allow a goto to jump out of the current procedure.

However, his construct is mainly useful for error handling, and the Object Pascal language provides a more general and structured mechanism to deal with errors: exception handling.


program Produce;

label 99;

procedure MyProc;
begin
  (*Something goes very wrong...*)
  goto 99;
end;

begin
  MyProc;
  99:
    Writeln('Fatal error');
end.

The example above tries to halt computation by doing a non-local goto.


program Solve;

uses SysUtils;

procedure MyProc;
begin
  (*Something goes very wrong...*)
  raise Exception.Create('Fatal error');
end;

begin
  try
    MyProc;
  except
    on E: Exception do Writeln(E.Message);
  end;
end.

In our solution, we used exception handling to stop the program. This has the advantage that we can also pass an error message. Another solution would be to use the standard procedures Halt or RunError.