Proper way to exit with specific exit code?

H. S. Teoh hsteoh at
Thu Sep 17 15:12:41 UTC 2020

On Thu, Sep 17, 2020 at 02:58:48PM +0000, drathier via Digitalmars-d-learn wrote:
> What's the proper way to exit with a specific exit code?
> I found a bunch of old threads discussing this, making sure
> destructors run and the runtime terminates properly, all of which
> seemingly concluding that it's sad that there isn't a way to do this
> easily, but hopefully things have changed in the last 5-10 years and
> I'm just missing the obvious solution.

AFAIK, there still isn't an "official" way to do this besides return
the exit code from main().  My go-to solution is to declare an
ExitException that's explicitly caught by main():

	class ExitException : Exception {
		int returnCode;
		this() { super("exit"); }

	void exit(int rc=0) { throw new ExitException(rc); }

	int main(string[] args) {
		try {
			... // your code here
		} catch (ExitException e) {
			return e.returnCode;
		return 0;

Caveat: this may or may not do the Right Thing(tm) in a multithreaded


Talk is cheap. Whining is actually free. -- Lars Wirzenius

More information about the Digitalmars-d-learn mailing list