RAD Studio |  |

Console Applications
See Also
Collapse All

Console applications are 32-bit programs that run without a graphical interface, in a console window. These applications typically don't require much user input and perform a limited set of functions. Any application that contains:

Copy Code

    {$APPTYPE CONSOLE}

in the code opens a console window of its own.

To create a new console application, choose FileNewOther. Select Delphi Projects and double-click Console Application from the New Items dialog box.

The IDE then creates a project file for this type of source file and displays the Code editor.

Console applications should make sure that no exceptions escape from the program scope. Otherwise, when the program terminates, the Windows operating system displays a modal dialog with exception information. For example, your application should include exception handling such as shown in the following code:

Copy Code

    program ConsoleExceptionHandling;
    {$APPTYPE CONSOLE}
    uses
    SysUtils;
    procedure ExecuteProgram;
    begin
      //Program does something
      raise Exception.Create('Unforeseen exception');
    end;
    begin
      try
        ExecuteProgram;
      except
    //Handle error condition
        WriteIn('Program terminated due to an exception');
        //Set ExitCode <> 0 to flag error condition (by convention)
        ExitCode := 1;
      end;
    end.

Users can terminate console applications in one of the following ways:

- Click the Close (X) button.
- Press Ctrl+C.
- Press Ctrl+Break.
- Log off.

Depending on which way the user chooses, the application is terminated forcefully, the process is not shut down cleanly, and the finalization section isn't run. Use the Windows API SetConsoleCtrlHandler function for options for handling these user termination requests.
See Also

TApplication

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!