Try Block Statements

A try statement is a protected block that can act special when an exception occurs. There are two types of try blocks, a try except and a try finally statement.

try except

try
  ...
  Body
  ..
except
  on e: Exception do
    Console.WriteLine('Error: '+e);
end;

Multiple on clauses are allowed, and a where can be used to filter an exception:

on e: SocketException where e.ErrorNo = 1000 do

The body is executed and if an exception occurs of the same type or a sub class, the catch block is executed and the exception won't pass further. raise; can be used rethrow the exception at the end of the catch. The type is optional, and when omitted all exceptions are caught.

try finally

try
  ...
  Body
  ...
finally
  Cleanup
end;

In this case the body of the try is executed, and the finally is always executed afterwards, if there's an exception the exception will be rethrown after the finally finishes, if there is none, the finally body runs as if it was placed after the body.

finally and except can be combined in 1 block, where the order defines which one will run first.