Try/Catch with version condition
Jacob Carlborg
doob at me.com
Tue Apr 9 18:27:21 UTC 2019
On 2019-04-09 19:07, aliak wrote:
> 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?
Here's an alternative, although "throw" is duplicated:
void call(lazy Exception func) {
version (foo) {
try {
throw func();
} catch (Exception e) {
writeln(e);
}
} else {
throw func();
}
}
void main()
{
call(new Exception("Boo"));
}
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list