www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Terminating an application

reply llee <llee goucher.edu> writes:
Is it possible to terminate an application after catching an exception|error? I
know how that assert will automatically terminate an application after catching
an AssertError. Is there a general method for terminating an application
outside of main(). Perhaps, something like exit() under linux? 
Sep 12 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
llee wrote:
 Is it possible to terminate an application after catching an exception|error?
I know how that assert will automatically terminate an application after
catching an AssertError. Is there a general method for terminating an
application outside of main(). Perhaps, something like exit() under linux? 

exit() is defined in std.c.stdlib. Selective import is good to avoid polluting your namespace with all the other cruft in stdlib: import std.c.stdlib : exit; --bb
Sep 12 2007
next sibling parent llee <llee goucher.edu> writes:
Bill Baxter Wrote:

 llee wrote:
 Is it possible to terminate an application after catching an exception|error?
I know how that assert will automatically terminate an application after
catching an AssertError. Is there a general method for terminating an
application outside of main(). Perhaps, something like exit() under linux? 

exit() is defined in std.c.stdlib. Selective import is good to avoid polluting your namespace with all the other cruft in stdlib: import std.c.stdlib : exit; --bb

Thanks.
Sep 12 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fc9oc2$2dsc$1 digitalmars.com...

 exit() is defined in std.c.stdlib.  Selective import is good to avoid 
 polluting your namespace with all the other cruft in stdlib:

Sadly, exit() will not unwind the stack, calling scope blocks and destroying scope instances. It also will not call any of the runtime cleanup functions (calling module dtors, closing the GC, anything else the RT wants to do) without explicitly telling it to with atexit(). So, it's not a very robust situation for the general case. I'd be _very_ interested how one could solve this without throwing an exception that you catch in main. Perhaps it's another runtime function that is necessary, one that'll unwind the stack but without an exception.
Sep 12 2007
parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:fc9q1j$2gr8$1 digitalmars.com...
<snip>
 Sadly, exit() will not unwind the stack, calling scope blocks and 
 destroying scope instances.  It also will not call any of the runtime 
 cleanup functions (calling module dtors, closing the GC, anything else the 
 RT wants to do) without explicitly telling it to with atexit().  So, it's 
 not a very robust situation for the general case.  I'd be _very_ 
 interested how one could solve this without throwing an exception that you 
 catch in main.  Perhaps it's another runtime function that is necessary, 
 one that'll unwind the stack but without an exception.

What's the point of trying to avoid using an exception? Stewart.
Sep 13 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:fccpeg$2qdo$1 digitalmars.com...

 What's the point of trying to avoid using an exception?

Exceptions in flight cause scope(failure) blocks to execute. Exiting an app deep in the call stack is not necessarily a failure condition.
Sep 13 2007
prev sibling parent reply Mike Parker <aldacron71 yahoo.com> writes:
llee wrote:
 Is it possible to terminate an application after catching an exception|error?
I know how that assert will automatically terminate an application after
catching an AssertError. Is there a general method for terminating an
application outside of main(). Perhaps, something like exit() under linux? 

If you rethrow the exception, or throw a new one, and let it propagate up the app will exit.
Sep 12 2007
parent Sean Kelly <sean f4.ca> writes:
Mike Parker wrote:
 llee wrote:
 Is it possible to terminate an application after catching an 
 exception|error? I know how that assert will automatically terminate 
 an application after catching an AssertError. Is there a general 
 method for terminating an application outside of main(). Perhaps, 
 something like exit() under linux? 

If you rethrow the exception, or throw a new one, and let it propagate up the app will exit.

Assuming the exception is thrown from the main thread. And assuming you're either using Phobos or are using Tango and have no non-daemon threads running. So far, the best alternative I've come up with is to register the routines you'd normally call manually when exiting a DLL with atexit, and then call the C exit routine. I've been considering doing basically this in a repackaged Runtime.exit(int) routine in Tango. Sean
Sep 12 2007