

Task

Show the syntax for a complete stoppage of a program inside a   conditional.

This includes all threads/processes which are part of your program.

Explain the cleanup (or lack thereof) caused by the termination (allocated memory, database connections, open files, object finalizers/destructors, run-on-exit hooks, etc.).

Unless otherwise described, no special cleanup outside that provided by the operating system is provided.




import core.stdc.stdio, core.stdc.stdlib;
 
extern(C) void foo() nothrow {
    "foo at exit".puts;
}
 
extern(C) void bar() nothrow {
    "bar at exit".puts;
}
 
extern(C) void spam() nothrow {
    "spam at exit".puts;
}
 
int baz(in int x) pure nothrow
in {
    assert(x != 0);
} body {
    if (x < 0)
        return 10;
    if (x > 0)
        return 20;
 
    // x can't be 0.
 
    // In release mode this becomes a halt, and it's sometimes
    // necessary. If you remove this the compiler gives:
    // Error: function test.notInfinite no return exp;
    //    or assert(0); at end of function
    assert(false);
}
 
// This generates an error, that is not meant to be caught.
// Objects are not guaranteed to be finalized.
int empty() pure nothrow {
    throw new Error(null);
}
 
static ~this() {
    // This module destructor is never called if
    // the program calls the exit function.
    import std.stdio;
    "Never called".writeln;
}
 
void main() {
    atexit(&foo);
    atexit(&bar);
    atexit(&spam);
 
    //abort(); // Also this is allowed. Will not call foo, bar, spam.
    exit(0);
}