www.fachtnaroe.ie



Web This site

Killing your program quickly

exit and die!

Exit can be used to exit a program quickly but relatively correctly, passing a numeric exit value. Die will do the same thing but passes a numeric or text value which can be trapped as an exception (ie flagged as an error).

When and why

Sometimes you'll want to end your program quickly. To do this there is the ideal, right, way:

The wrong way - generally - is to terminate or abort the program suddenly. Many modern languages provide at least one command for this. This does not do any tidy-up that may be required of the program, and may lead to memory leaks, files left open or other unexpected issues. In perl, use the command die to kill the program instantly raising an exception; or exit to, well, exit instantly.

But don't forget to ask yourself, if this is the wrong thing to do, why are there two separate ways to do this? Because this may be seen as the "wrong but right in the circumstances" way.


Last updated: 20120219-18:06
back to top
Fachtna Roe, Senior College, Central Technical Institute, Clonmel, Ireland.