Observing exceptions in a destructor

Mark Isaacson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 21 18:02:15 PDT 2015


I'd like to be able to know if my destructor is being called 
because an exception was thrown. Any way to do that?

I tried this: http://ideone.com/JbXH2w

(Pasted here for convenience):

import std.stdio, std.exception;

struct Catcher {
	~this() {
		try {} catch (Exception e) {
			writeln("Woooo!");
			throw e;
		}
		scope (failure) {
			writeln("Woooo woooo!");
		}
		writeln("Destructing...");
	}
}

void main() {
	scope (failure) writeln("Sensible");
	scope (exit) writeln("Always written");
	Catcher c1;
	scope auto c2 = Catcher();
	throw new Exception("Foobar");
}

Which does not print the "Wooo" lines, but does print all the 
others.


More information about the Digitalmars-d-learn mailing list