Free Pascal supports exceptions. Exceptions are useful for error handling and avoiding resource leaks. However, bear in mind that exceptions have a performance impact.
The official documentation is here: Reference guide chapter 17.
By default exceptions are disabled. You can opt in by using the ObjFPC or DELPHI Compiler Mode, or adding -Sx to the compiler commandline. This enables the
finally reserved words for use in your own code, but it doesn't enable exception generation from the RTL. To enable the RTL to raise exceptions instead of generating runtime errors, use the SysUtils unit in your program.
SysUtils automatically sets up a basic exception catcher, so any otherwise uncaught exception is displayed in human-readable form, as the program terminates. To generate readable callstacks from caught exceptions in your own code, without necessarily terminating the program, you can use SysUtils functions
Note that Pascal uses different keywords than some other languages:
raise instead of
except instead of
catch. Also, as a compiler design choice, each
try-block can pair with either an
finally block, but not both at the same time. You'll need to nest
try-blocks if you need
finally protecting the same code.
uses sysutils; begin try // Do something that might go wrong. except begin // Try to recover or show the user an error message. end; end; end.
Cleaning up resources
try // Do something that might go wrong. finally // Clean-up code that is always called even if an exception was raised. end;
Finally-blocks don't destroy exception objects. Any exception that reaches the program's "end." will trigger a memory leak warning. An extra except block can be used to consume such exceptions. This is Delphi-compatible.
begin try // Your main program, where an exception object is created. finally try // Clean-up code. except end; end; end.
raise Exception.Create('Helpful description of what went wrong');
Using specialised exception types to signal different problems
type EMyLittleException = Class(Exception); begin try raise EMyLittleException.Create('Foo'); except on E : EMyLittleException do writeln(E.Message); on E : Exception do writeln('This is not my exception!'); else writeln('This is not an Exception-descendant at all!'); end; end;
try // Do something that might go wrong. except // Try to recover or show the user an error message. if recoveredSuccessfully = FALSE then raise; end;
SysUtils defines and raises many specific exception classes.
With SysUtils included in your program, and exceptions enabled, various runtime errors are changed into exceptions. Processor-level interrupts like SIGSEGV or SIGFPU, which normally translate to run-time errors, are also changed to exceptions. For example, run-time error 200 (division by zero) becomes EDivByZero or EZeroDivide, while run-time error 216 (a general protection fault) becomes EAccessViolation.
- Raise exceptions to signal that an operation could not be completed, where it normally should have succeeded.
- Do not use exceptions as part of expected control flow. Instead, add checks for common error conditions and return error values the old-fashioned way. For example, input parsing problems or file existence fails are usually not truly exceptional.
- But do raise an exception if it's critical that the error is noticed; programmers may forget to check for returned error values.
- Naming convention: Prefix exception class names with a capital E.
- Re-raise exceptions in
raise;if you were unable to recover from the problem; this preserves the original exception's callstack.
- Be careful about using the catch-all base
Exceptionclass, since the underlying code or OS/filesystem might produce an unanticipated exception that slips through your exception handler, potentially corrupting the program state.
- When writing a unit or library, if exceptions are used at all, they should be documented clearly up front. This way the unit user knows what to expect.
- Keep exception handling away from code that needs to run as fast as possible.
Compiler optimisation levels don't make much difference. All exception blocks use a small amount of wrapper code that can't be optimised away. There are different ways for a compiler to produce exception code, with varying performance implications. As of FPC 3.0.4, the default exception mechanism uses the standard setjmp/longjmp style. FPC also supports OS-provided Structured Exception Handling; this is the default on Win64, and can be enabled on Win32 (recompile the compiler with
$define TEST_WIN32_SEH). Other mechanisms may be added to FPC in the future.
To get a better feel for what's going on, try writing a small test program and compiling it with the
-a switch. This leaves behind a human-readable assembly file.
Notes on the setjmp/longjmp method:
Try-statements insert some extra address calculations, a stack push and pop, some direct jumps, and a conditional jump. This is the setup cost of an exception frame, even if no exception is raised.
Except-statements insert the above, plus a push and pop, more direct jumps, and another conditional jump.
Finally-statements add a pop and a conditional jump.
Raise-statements create a string and pass control to FPC's exception raiser. The string creation itself can spawn an implicit exception frame for the function/method, due to dynamic string allocations, even if the
raiseis not executed. This is why in e.g. container types one often sees the
raisemoved to a separate local (private) procedure. This localises the exception frame to that procedure, so the performance hit is only taken if the procedure is called.
Furthermore, generating a human-readable callstack from a raised exception involves time-consuming stack traversal and string manipulation.
With all that said, outside of heavy processing code, the convenience of exceptions usually outweighs their performance impact. Don't feel bad about using them if it makes your code better.
Exceptions vs Error codes - a situation where exceptions might be a danger in some code (see middle of this page)