Jessica’s Delphi Quick Reference

Looping & Conditionals

If/Then

if cond then stmt;
if cond then stmt else stmt;

For Loop

for i := 0 to 5 do
  stmt;
for i := 5 downto 0 do
  stmt;

For-In Loop

for n in numbers do
  stmt;

Do While

repeat code; until condition;

Do Loop

while condition begin code; end;

Early Termination:

break;    //Exit for/repeat/while loop
continue; //Resume at next iteration
exit;     //Exit current procedure

Case (“Switch”)

case selector of
1..5: code;
6,8:  code;
7:    code;
else  code;
end;

Exception Handling

Try-Except

  • Except catches exceptions to prevent them from “bubbling up”
try stmts; except stmt; end;
try
  stmts;
except
  on E : Exception do
    ShowMessage(E.ClassName+':' + E.Message);
end;
try
  ...
except
  on EZeroDivide do HandleZeroDivide;
  on EOverflow do HandleOverflow;
  on EMathError do HandleMathError;
else
  HandleAllOthers;
end;
  • E.ClassName gives the exception type
  • E.Message gives the exception detail msg

Try-Finally

  • Finally does not trap errors.
  • Used to free memory, close files/sockets/etc.
try stmts; finally stmt; end;

Create a new Exception Type

type EFooError = class(Exception);
type EBarError = class(Exception)
  ErrorCode: Integer;
end;

Throw an exception

raise EMathError.Create;
raise Exception.Create('Missing parameter');

Raise an Error

try
   ...
except
  raise;
end;