Try/Catch with version condition

Sebastiaan Koppe mail at skoppe.eu
Tue Apr 9 17:08:59 UTC 2019


On Tuesday, 9 April 2019 at 14:43:54 UTC, foo wrote:
> But then that requires too much code duplication.
> What would be the best way to do this?

I like this approach:

---

import std.stdio;

void tryIt(T)(lazy T t) {
     version (foo)
	try {
     	    t();
	} catch (Exception e) {
	    writeln(e);
	}
     else
         t();
}

void whatever() {
     throw new Exception("Hello");
}

void main() {
     whatever.tryIt();
}

---


More information about the Digitalmars-d mailing list