Difference between revisions of "AddExitProc"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎See also: Fixed page link)
 
Line 1: Line 1:
{{LanguageBar}}
+
{{AddExitProc}}
  
 
== AddExitProc ==
 
== AddExitProc ==
Line 57: Line 57:
  
 
* [[ExitProc]] and [[ExitProc#ExitCode|ExitCode]] - intercept the end of the program and evaluate ExitCode.
 
* [[ExitProc]] and [[ExitProc#ExitCode|ExitCode]] - intercept the end of the program and evaluate ExitCode.
 
[[Category:Pascal]]
 

Latest revision as of 00:49, 20 May 2020

Deutsch (de) English (en)

AddExitProc

With AddExitProc, you can assign a procedure that is called when the program ends; even when terminated by a runtime error.

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.

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

program AddExitProc_Example;

 const
   MyExitCode = 123;

   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;
   end;

 var
   f : file;

 begin
   // Assign new ExitProc
   AddExitProc(@end);

   // Open a file that doesn't exist
   Assign(f, 'abc.txt');

   // This triggers runtime error 2
   Reset(f);

   // Cancel with your own code.
   Halt(MyExitCode);
 end.

See also