Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 15, 2014 How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement? |
September 15, 2014 Re: How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | how about "return"? :)
there is also "assert"... and pls note, scope could also be your friend :O
On Monday, 15 September 2014 at 23:36:56 UTC, AsmMan wrote:
> Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement?
|
September 15, 2014 Re: How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Mon, Sep 15, 2014 at 11:36:54PM +0000, AsmMan via Digitalmars-d-learn wrote: > Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement? AFAIK, there is currently no replacement. I personally use an ExitException and put a catch block in main(): class ExitException : Exception { int status; this(int _status=0, string file=__FILE__, size_t line=__LINE__) { super("Program exit", file, line); status = _status; } } void exit(int status=0) { throw new ExitException(status); } ... int main() { try { ... } catch(ExitException e) { return e.status; } return 0; } The catch is that this may or may not work correctly in multithreaded programs, because the exception may happen in a different thread than the one main() is running in, and there isn't any nice way to terminate other still-running threads after catching such an exception. There has some discussion as to how to implement this, but AFAIK no good solution was found. See also: https://issues.dlang.org/show_bug.cgi?id=3462 But at least, for single-threaded programs, the above ExitException should work reasonably well. T -- Almost all proofs have bugs, but almost all theorems are true. -- Paul Pedersen |
September 16, 2014 Re: How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | On Monday, 15 September 2014 at 23:42:02 UTC, notna wrote:
> how about "return"? :)
>
> there is also "assert"... and pls note, scope could also be your friend :O
>
> On Monday, 15 September 2014 at 23:36:56 UTC, AsmMan wrote:
>> Someone said somewhere that call std.c.process.exit() isn't the proper way to exit from a D program since it doesn't terminate some phobos stuff. So what should I use instead of? or there's no a replacement?
Neither assert or return will help. Check out this code example:
void main() {
f();
}
void f() {
if(!foo)
exit(1);
do_something();
}
|
September 16, 2014 Re: How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Monday, 15 September 2014 at 23:52:25 UTC, H. S. Teoh via
Digitalmars-d-learn wrote:
> On Mon, Sep 15, 2014 at 11:36:54PM +0000, AsmMan via Digitalmars-d-learn wrote:
>> Someone said somewhere that call std.c.process.exit() isn't the proper
>> way to exit from a D program since it doesn't terminate some phobos
>> stuff. So what should I use instead of? or there's no a replacement?
>
> AFAIK, there is currently no replacement. I personally use an
> ExitException and put a catch block in main():
>
> class ExitException : Exception {
> int status;
> this(int _status=0, string file=__FILE__, size_t
> line=__LINE__)
> {
> super("Program exit", file, line);
> status = _status;
> }
> }
> void exit(int status=0) {
> throw new ExitException(status);
> }
> ...
> int main() {
> try {
> ...
> } catch(ExitException e) {
> return e.status;
> }
> return 0;
> }
>
> The catch is that this may or may not work correctly in multithreaded
> programs, because the exception may happen in a different thread than
> the one main() is running in, and there isn't any nice way to terminate
> other still-running threads after catching such an exception.
>
> There has some discussion as to how to implement this, but AFAIK no good
> solution was found. See also:
>
> https://issues.dlang.org/show_bug.cgi?id=3462
>
> But at least, for single-threaded programs, the above ExitException
> should work reasonably well.
>
>
> T
Thanks! I'll use it.
|
September 16, 2014 Re: How do I properly exit from a D program (outside main)? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On 16/09/14 02:06, AsmMan wrote: > Neither assert or return will help. Check out this code example: > > void main() { > f(); > } > > void f() { > if(!foo) > exit(1); > do_something(); > } If you want to exit the application with exit code 1 then it sounds like something has gone wrong. In that case, throw an exception instead. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation