Ad Space — Top Banner

E2047

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

E2047 means Delphi expected to see the BEGIN keyword to open a code block but found something else instead. This usually means a BEGIN is missing before a series of statements inside an if, for, while, try, or procedure body. Fix it by adding the BEGIN keyword in the correct position and its matching END to close the block.

Affected Models

  • Delphi 10.4 Sydney
  • Delphi 11 Alexandria
  • Delphi 12 Athens
  • Embarcadero RAD Studio
  • Free Pascal / Lazarus

Common Causes

  • Writing multiple statements after if...then without wrapping them in begin...end
  • Missing the begin keyword at the start of a procedure or function body
  • Accidentally deleting a begin while refactoring code
  • Writing case...of without a begin for a multi-statement branch
  • Missing begin after a for or while loop where multiple statements follow

How to Fix It

  1. Go to the line the IDE highlights and look at the context — identify whether this is inside an if, for, while, try, or a procedure/function body.

    The BEGIN is required to group multiple statements where the language expects a single statement.

  2. Add 'begin' before the first statement of the block and 'end;' (or 'end' followed by the appropriate keyword) after the last statement.

    Example: if Condition then begin Statement1; Statement2; end;

  3. For procedure and function bodies, BEGIN is always required — there is no single-statement shorthand for procedure bodies.

    Every procedure and function must start with begin and end with end;

  4. Check case...of branches — each branch with multiple statements needs its own begin...end block.

    Example: case X of 1: begin DoThis; DoThat; end; 2: DoSomethingElse; end;

  5. Use the IDE's code formatter (Edit > Format Source) to re-indent the code after adding begin...end — this helps verify that blocks are properly matched.

    Mismatched begin...end pairs are easier to spot with consistent indentation.

When to Call a Professional

This is a compiler error — no professional repair needed. Fix it by reading the error message and correcting the code. The Delphi IDE highlights the exact line causing the problem.

Frequently Asked Questions

Do I always need begin...end after if...then?

No — if you only have a single statement after if...then, you can omit begin...end. Example: if X > 0 then DoSomething; is valid without begin...end. However, for two or more statements you must use begin...end to group them. Many Delphi developers always use begin...end for consistency to avoid accidental bugs.

What is the rule for begin...end with else?

When you have if...then...else and want multiple statements in the then or else branch, each branch needs its own begin...end. Example: if X > 0 then begin A; B; end else begin C; D; end; The semicolons before the else are not required but allowed.

Can the IDE automatically insert begin...end for me?

The Delphi IDE has code templates and completion features but does not automatically insert begin...end when you are missing one. However, when you type 'begin' and press Enter, the IDE may auto-complete the matching 'end' depending on your IDE settings. The best practice is to type begin and end together before filling in the statements between them.