You can tell the global application object to terminate the program by calling Application.Terminate .
End call to end the program. Call Terminate, and do not release the application object, you allow closing the application in an orderly manner .
End a call to the Windows PostQuitMessage function to perform an orderly shutdown of the application . Completion does not occur immediately.
Since the call can go deeper on the stack, you can also raise an exception , and you code your program so that it doesn't run in the order that the execution reaches the main loop of the application and the exception handler catches it by default.
This way you effectively prevent more code from running in your application.
In code, it might look like this:
if not FileExists(FilePath1) then begin MessageDlg(FilePath1 + ' does not exist and is required for this program to function.', mtWarning, [mbOK], 0); Application.Terminate; Abort;
Depending on where this code is called, I advise you not to show the message directly, but rather to raise an exception with the message and let the default HandleException method of the application object show the message for you:
if not FileExists(FilePath1) then begin Application.Terminate; raise EMyFatalException.Create(FilePath1 + ' does not exist and is required for this program to function.'); end;
Which looks more natural to me. EMyFatalException is a hypothetical exception class that you can declare and not handle in your except clauses.
source share