Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminate Program Upon Error 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
One of the problems I've always had (and I'm running into it again) is how to terminate a program based on an error. Basically, in a piece of code (foreign to Application.Terminate because all my searches are leading there, this code knows nothing of TApplication and shouldn't), I run into a condition that requires the program cease. How do I do that?

Code:
if (problem condition that borks the program if the code continues) then
  begin
    raise Exception.Create('There's a problem that will bork the program if this continues.');
    (Kill the program here, basically I'm wanting to produce a crash condition)
  end;

I've tried everything I can think of after that raise line and the program merrily goes on its way and runs as if everything is alright.

So how do I kill this program when it produces this error?

 
Just use :
Code:
if (problem condition that borks the program if the code continues) then
  begin
    MessageDlg('There's a problem that will bork the program if this continues.', mtError, [mbOK], 0);
    ExitProcess(0) 
  end;

/Daddy



-----------------------------------------------------
Helping people is my job...
 
Okay. Your answer made me look into "raise" a little more. It turns out, "raise" does stop the code, but for that module only. So no code ever gets run at all after that line.

"For that module only" becomes the problem as this is in a Create constructor for an object in another unit outside of the main program. So in the main program, the logic is treated as if the Create constructor ran normally. So the form comes up and all that.

Replacing the exception with a message dialog, e.g.

Code:
MessageDlg('There's a problem that will bork the program if this continues.', mtError, [mbOK], 0);

Will then enable the next line to run, wherein ExitProcess(0) works properly.

It seems either something is inappropriate with "raise" somehow, or I'm not understanding the use of exceptions, especially if they won't crash/abend programs at all.

Thanks for your help.
 
There are several Windows API functions you could use to notify a parent process that the child process has crapped out. The Exception model is only for use within a process; DLL's can use exceptions to notify calling processes because they use the same stack frame and memory space. To properly use an exception the way you want to, use a catch (except on exception block in Delphi) to catch the exception and process the actual termination code. something like:

Code:
...
// from your code
if (problem condition that borks the program if the code continues) then
  begin
    raise Exception.Create('There's a problem that will bork the program if this continues.');
  end; 
 except  // begin exception handler section
  on Exception FatalAppExit(0,Exception.Message);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top