Quick start manual
Classes and objects
7-27
Exceptions
The defining declaration of a class method must also begin with class. For example,
class procedure TFigure.GetInfo(var Info: TFigureInfo);
begin
ƒ
end;
In the defining declaration of a class method, the identifier Self represents the class
where the method is called (which could be a descendant of the class in which it is
defined). If the method is called in the class C, then Self is of the type class of C. Thus
you cannot use Self to access fields, properties, and normal (object) methods, but you
can use it to call constructors and other class methods.
A class method can be called through a class reference or an object reference. When it
is called through an object reference, the class of the object becomes the value of Self.
Exceptions
An exception is raised when an error or other event interrupts normal execution of a
program. The exception transfers control to an exception handler, which allows you to
separate normal program logic from error-handling. Because exceptions are objects,
they can be grouped into hierarchies using inheritance, and new exceptions can be
introduced without affecting existing code. An exception can carry information, such
as an error message, from the point where it is raised to the point where it is handled.
When an application uses the SysUtils unit, most runtime errors are automatically
converted into exceptions. Many errors that would otherwise terminate an
application—such as insufficient memory, division by zero, and general protection
faults—can be caught and handled.
When to use exceptions
Exceptions provide an elegant way to trap runtime errors without halting the
program and without awkward conditional statements. The requirements imposed
by exception handling semantics impose a code/data size and runtime performance
penalty. While it is possible to raise exceptions for almost any reason, and to protect
almost any block of code by wrapping it in a try...except or try...finally statement, in
practice these tools are best reserved for special situations.
Exception handling is appropriate for errors whose chances of occurring are low or
difficult to assess, but whose consequences are likely to be catastrophic (such as
crashing the application); for error conditions that are complicated or difficult to test
for in if...then statements; and when you need to respond to exceptions raised by the
operating system or by routines whose source code you don’t control. Exceptions are
commonly used for hardware, memory, I/O, and operating-system errors.