Try/Catch with version condition
aliak
something at something.com
Tue Apr 9 17:07:27 UTC 2019
On Tuesday, 9 April 2019 at 15:03:03 UTC, Adam D. Ruppe wrote:
> 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'd simply put the try {} body into a helper function, then
> make the versions just call that function.
>
>
> void helper() {
> throw new Exception("");
> }
>
> version(foo) {
> try helper();
> catch(Exception) {}
> } else {
> helper();
> }
>
>
> Remember that with D's nested functions, you can define helper
> functions just about anywhere and containing the same stuff
> you'd use directly.
Aye, I'd do the same. And if you want to generalize it then maybe
something like:
void call(alias func)() {
version (foo) {
try {
func();
} catch (Exception e) {
writeln(e);
}
} else {
func();
}
}
void main() {
call!(() { throw new Exception("Boo"); } );
}
On a side note, this doesn't work: "call!(() => throw new
Exception("Boo"))". Is there any reason throw *has* to be a
statement and not an expression?
More information about the Digitalmars-d
mailing list