AddExitProc: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: AddExitProc
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## AddExitProc | |
| 2 | + | ||
| 3 | + | With AddExitProc, you can assign a procedure that is called when the program ends; even when terminated by a runtime error. | |
| 4 | + | ||
| 5 | + | Compared to the simple [[ExitProc]], this has the advantage that you don't have to worry about saving and calling up the old ExitProc, this happens automatically in the background with AddExitProc. | |
| 6 | + | ||
| 7 | + | In this example, a file that does not exist is deliberately opened: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | program AddExitProc_Example; | |
| 11 | + | ||
| 12 | + | const | |
| 13 | + | MyExitCode = 123; | |
| 14 | + | ||
| 15 | + | procedure end; | |
| 16 | + | begin | |
| 17 | + | WriteLn('This is the end'); | |
| 18 | + | ||
| 19 | + | case ExitCode of | |
| 20 | + | 2: | |
| 21 | + | begin | |
| 22 | + | WriteLn('file not found'); | |
| 23 | + | end; | |
| 24 | + | ||
| 25 | + | MyExitCode: | |
| 26 | + | begin | |
| 27 | + | WriteLn('It was deliberately terminated with ', MyExitCode); | |
| 28 | + | end; | |
| 29 | + | else | |
| 30 | + | begin | |
| 31 | + | WriteLn('Error: ', ExitCode); | |
| 32 | + | end; | |
| 33 | + | end; | |
| 34 | + | end; | |
| 35 | + | ||
| 36 | + | var | |
| 37 | + | f : file; | |
| 38 | + | ||
| 39 | + | begin | |
| 40 | + | // Assign new ExitProc | |
| 41 | + | AddExitProc(@end); | |
| 42 | + | ||
| 43 | + | // Open a file that doesn't exist | |
| 44 | + | Assign(f, 'abc.txt'); | |
| 45 | + | ||
| 46 | + | // This triggers runtime error 2 | |
| 47 | + | Reset(f); | |
| 48 | + | ||
| 49 | + | // Cancel with your own code. | |
| 50 | + | Halt(MyExitCode); | |
| 51 | + | end. | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | ## See also | |
| 55 | + | ||
| 56 | + | - [[ExitProc]] and [[ExitProc|ExitCode]] - intercept the end of the program and evaluate ExitCode. | |
| 57 | + | ||
| 58 | + | --- | |
| 59 | + | *From the Free Pascal wiki, page "AddExitProc", 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).* |
