Raising an Exception

From RAD Studio
Jump to: navigation, search

Go Up to Writing the Try Block


To indicate a disruptive error condition, you can raise an exception by constructing an instance of an exception object that describes the error condition and calling the reserved word raise.

To raise an exception, call the reserved word raise, followed by an instance of an exception object. This establishes the exception as coming from a particular address. When an exception handler actually handles the exception, it finishes by destroying the exception instance, so you never need to do that yourself.

For example, given the following declaration,

type
  EPasswordInvalid = class(Exception);

you can raise a "password invalid" exception at any time by calling raise with an instance of EPasswordInvalid, like this:

if Password <> CorrectPassword then
  raise EPasswordInvalid.Create('Incorrect password entered');

Raising an exception sets the ErrorAddr variable in the System unit to the address where the application raised the exception. You can refer to ErrorAddr in your exception handlers, for example, to notify the user where the error occurred. You can also specify a value in the raise clause that appears in ErrorAddr when an exception occurs.

Warning: Do not assign a value to ErrorAddr yourself. It is intended as read-only.

To specify an error address for an exception, add the reserved word at after the exception instance, followed by an address expression such as an identifier.

See Also