ExitProc: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ExitProc
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # ExitProc | |
| 2 | + | ||
| 3 | + | With ExitProc, you can assign a procedure that is called at the end of the program; even when terminated by a runtime error. | |
| 4 | + | ||
| 5 | + | It is recommended to save the old ExitProc and to call it from the new ExitProc. | |
| 6 | + | ||
| 7 | + | ## Example | |
| 8 | + | ||
| 9 | + | In this example, a file that does not exist is deliberately opened. | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | program ExitProc_Example; | |
| 13 | + | ||
| 14 | + | const | |
| 15 | + | MyExitCode = 123; | |
| 16 | + | ||
| 17 | + | var | |
| 18 | + | OldExitProc : Pointer; | |
| 19 | + | ||
| 20 | + | procedure end; | |
| 21 | + | begin | |
| 22 | + | WriteLn('This is the end'); | |
| 23 | + | ||
| 24 | + | case ExitCode of | |
| 25 | + | 2: | |
| 26 | + | begin | |
| 27 | + | WriteLn('file not found'); | |
| 28 | + | end; | |
| 29 | + | ||
| 30 | + | MyExitCode: | |
| 31 | + | begin | |
| 32 | + | WriteLn('It was deliberately terminated with ', MyExitCode); | |
| 33 | + | end; | |
| 34 | + | ||
| 35 | + | else | |
| 36 | + | begin | |
| 37 | + | WriteLn('Error: ', ExitCode); | |
| 38 | + | end; | |
| 39 | + | end; | |
| 40 | + | ||
| 41 | + | ExitProc := OldExitProc; | |
| 42 | + | end; | |
| 43 | + | ||
| 44 | + | var | |
| 45 | + | f : file; | |
| 46 | + | ||
| 47 | + | begin | |
| 48 | + | // Save old ExitProc | |
| 49 | + | OldExitProc := ExitProc; | |
| 50 | + | ||
| 51 | + | // Assign new ExitProc. | |
| 52 | + | ExitProc := @end ; | |
| 53 | + | ||
| 54 | + | // Open a file that doesn't exist | |
| 55 | + | Assign(f, 'abc.txt'); | |
| 56 | + | ||
| 57 | + | // This triggers runtime error 2 | |
| 58 | + | Reset(f); | |
| 59 | + | ||
| 60 | + | // Terminate with your own exit code | |
| 61 | + | Halt(MyExitCode); | |
| 62 | + | end. | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | # ExitCode | |
| 66 | + | ||
| 67 | + | ExitCode specifies the error code with which the program terminates. Usually, if ExitCode is 0, the program ended normally. | |
| 68 | + | ||
| 69 | + | ## Terminated due to runtime error | |
| 70 | + | ||
| 71 | + | In this example, if the memory field is exceeded, the exit code is 201 (range check error). | |
| 72 | + | ||
| 73 | + | ``` pascal | |
| 74 | + | var | |
| 75 | + | a : array[0 .. 3] of byte; | |
| 76 | + | ||
| 77 | + | begin | |
| 78 | + | OldExitProc := ExitProc; | |
| 79 | + | ExitProc := @end; | |
| 80 | + | ||
| 81 | + | // memory field deliberately exceeded | |
| 82 | + | a[4] := 12; | |
| 83 | + | ``` | |
| 84 | + | ||
| 85 | + | ## Forced termination | |
| 86 | + | ||
| 87 | + | If you force a program termination with Halt(), ExitProc is still called. | |
| 88 | + | ||
| 89 | + | If you Halt(num), then num is interpreted as an exit code. In this example, the exit code is 123. | |
| 90 | + | ||
| 91 | + | ``` pascal | |
| 92 | + | Halt(123); | |
| 93 | + | ``` | |
| 94 | + | ||
| 95 | + | # See also | |
| 96 | + | ||
| 97 | + | - [[AddExitProc]] - Add ExitProc procedure at program end. | |
| 98 | + | ||
| 99 | + | --- | |
| 100 | + | *From the Free Pascal wiki, page "ExitProc", as it stood on 2020-05-19 after a change by FTurtle, out of the wiki's dump of 2024-05-26. 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](/wiki-contact).* |
