适当的灾难性错误处理

Gle*_*234 4 delphi error-handling delphi-2006 delphi-3

有一些我一直在努力的东西,我真的没有用Delphi程序解决,并想知道是否有人可以指导我.正如主题所说,你如何做正确的灾难性错误处理?例如:

// is file necessary for the program present?
if not FileExists(FilePath1) then
   begin
     raise Exception.Create(FilePath1 + ' does not exist and is required for this program to function.');
   // I obviously need to do something here to make the program QUIT and not have
   // any more code run.

     Application.Terminate;
     Abort;
   end;
Run Code Online (Sandbox Code Playgroud)

我也可以在那里使用异常单元并抛出异常,但程序继续像以前一样.我过去曾经使用过停止呼叫,但它似乎没有进行任何清理等等,所以我最终制定了一个大程序,通过近距离和免费调用我所做的一切就是为了确保(即使那时我我不确定任何幕后的东西.

那么处理这类事情的正确方法是什么?

编辑:为了澄清,我想知道如何让程序做它需要做的清理,然后立即退出而不做任何其他代码.

Dav*_*nan 6

要执行异常终止,请调用Halt()传递退出代码.

if CatastropicErrorDetected then
begin
  ... show error message
  Halt(1);
end;
Run Code Online (Sandbox Code Playgroud)

在Windows上,这会导致调用TerminateProcess并在那时停止执行.

您注意到没有执行清理,通常这就是您想要的.由于您在应用程序启动时执行此检查,因此无需清理任何内容.