Sign in Join
Wiki copytemporary

ExitProc

Deutsch · English

Contents

ExitProc

With ExitProc, you can assign a procedure that is called at the end of the program; even when terminated by a runtime error.

It is recommended to save the old ExitProc and to call it from the new ExitProc.

Example

In this example, a file that does not exist is deliberately opened.

pascal
program ExitProc_Example;
    
     const
       MyExitCode = 123;
    
     var
       OldExitProc : Pointer;
    
       procedure end;
       begin
         WriteLn('This is the end');
    
         case ExitCode of
           2: 
             begin
               WriteLn('file not found');
             end;
    
           MyExitCode:
             begin
               WriteLn('It was deliberately terminated with ', MyExitCode);
             end;
    
           else 
             begin
               WriteLn('Error: ', ExitCode);
             end;
         end;
    
         ExitProc := OldExitProc;
       end;
    
     var
       f : file;
    
     begin
       // Save old ExitProc
       OldExitProc := ExitProc;
    
       // Assign new ExitProc.
       ExitProc := @end ;
    
       // Open a file that doesn't exist
       Assign(f, 'abc.txt');
    
       // This triggers runtime error 2
       Reset(f);
    
       // Terminate with your own exit code
       Halt(MyExitCode);
     end.

ExitCode

ExitCode specifies the error code with which the program terminates. Usually, if ExitCode is 0, the program ended normally.

Terminated due to runtime error

In this example, if the memory field is exceeded, the exit code is 201 (range check error).

pascal
var
       a : array[0 .. 3] of byte;
    
     begin
       OldExitProc := ExitProc;
       ExitProc := @end;
    
       // memory field deliberately exceeded
       a[4] := 12;

Forced termination

If you force a program termination with Halt(), ExitProc is still called.

If you Halt(num), then num is interpreted as an exit code. In this example, the exit code is 123.

pascal
Halt(123);

See also


From the Free Pascal wiki, page "ExitProc", as the Internet Archive kept it on 2024-03-04, last changed on 2020-05-20. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.