
When an address is specified, it can be any expression that evaluates to a pointer type, but is usually a pointer to a procedure or function. Where object and at address are both optional. In general, the form of a raise statement is To raise an exception object, use an instance of the exception class with a raise statement. Given these declarations, you can define a single SysUtils.EMathError exception handler that also handles SysUtils.EInvalidOp, SysUtils.EZeroDivide, SysUtils.Overflow, and SysUtils.EUnderflow.Įxception classes sometimes define fields, methods, or properties that convey additional information about the error. For example, the following declarations in SysUtils define a family of exception types for math errors: You can group exceptions into families using inheritance. In fact, it is possible to use an instance of any class as an exception, but it is recommended that exceptions be derived from the SysUtils.Exception class defined in SysUtils. Assertions should be used only to test for conditions that you do not expect to occur.Įxception types are declared just like other classes. When an Assert statement fails, the program either halts with a runtime error or (if it uses the SysUtils unit) raises an SysUtils.EAssertionFailed exception. If FileExists(FileName) then // returns False if file is not found raises no exceptionĪssertions provide another way of testing a Boolean condition anywhere in your source code.

Reset(F) // raises an EInOutError exception if file is not foundīut you could also avoid the overhead of exception handling by using: For example, suppose you want to make sure that a file exists before trying to open it. Exceptions are commonly used for hardware, memory, I/O, and operating-system errors.Ĭonditional statements are often the best way to test for errors. 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.Įxception 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. The requirements imposed by exception handling semantics impose a code/data size and runtime performance penalty. Many errors that would otherwise terminate an application - such as insufficient memory, division by zero, and general protection faults - can be caught and handled.Įxceptions provide an elegant way to trap runtime errors without halting the program and without awkward conditional statements. When an application uses the SysUtils unit, most runtime errors are automatically converted into exceptions. An exception can carry information, such as an error message, from the point where it is raised to the point where it is handled. Because exceptions are objects, they can be grouped into hierarchies using inheritance, and new exceptions can be introduced without affecting existing code. The exception transfers control to an exception handler, which allows you to separate normal program logic from error-handling.

5 Standard Exception Classes and RoutinesĪn exception is raised when an error or other event interrupts normal execution of a program.
