INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Terminate Program Upon Error

Terminate Program Upon Error

(OP)
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?

RE: Terminate Program Upon 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...

RE: Terminate Program Upon Error

(OP)
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.

RE: Terminate Program Upon Error

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close