Add a clean way to exit a process

Leandro Lucarella llucax at gmail.com
Sat Oct 31 22:30:38 PDT 2009


Hi. I won't be very verbose about this because I submitted a bug about it
(with a patch):
http://d.puremagic.com/issues/show_bug.cgi?id=3462

But I think it might deserve some public discussion too, that's why I'm
bringing it here.

For the lazy, here is the bug report text:

Maybe I'm missing something, but I can't find any "standard" way to exit a
program cleanly. I can't just call C's exit() function because the stack
doesn't get unwinded so scope guards and other finally blocks are not executed.
This is bad if you want to do some cleanup. In my particular case, I create a
lock file so other instances of a program exit immediately if the lock file is
present, and I want to remove the lock file as soon as the program finishes. I
want to be able to call some exit() function in any part of the program for
simplicity though.

I hacked a solution by adding all the program inside a try block, catching an
special "Exit" exception with a status attribute. If that exception is catched,
the program returns the exception's status code.

I think this is an useful feature that deserves being in the standard library,
and for that we need runtime support (that's why I added it to the druntime
component instead of Phobos, even when Phobos should be hacked too.

Attached are patches for druntime and phobos with this changes:

druntime:
* Add a ProcessExit class to core.exception module (inherits from Object since
it's not a real exception and we don't want people catching it even when doing
a catch (Throwable)).
* Catch the new exception in tryExec() nested funtion from dmain2.d, even when
rt_trapExceptions is false (again, because is not really an exception), making
the status attribute the new program's exit code.

phobos:
* Add a new void std.process.exit(int status). All it does is "throw new
ProcessExit(status);" to hide implementation details.

I don't know if std.process is the better module, maybe it should go somewhere
else.


If this is accepted, I guess the discussion in this thread should be revised:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=99432

Personally I think:

--------------------
try {
    // code
}
catch {
    // code
}
--------------------

Should be a shortcut to:

--------------------
try {
    // code
}
catch (Exception) {
    // code
}
--------------------


Thanks!

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
For me to ask a woman out, I've got to get into a mental state like the karate
guys before they break the bricks.
	-- George Constanza



More information about the Digitalmars-d mailing list