On Tuesday, 22 May 2018 at 18:20:43 UTC, Robert M. Münch wrote:
I see that I'm writing

try {
... different code ...
} catch (myException e) {
        ... same handling code ...
}

over and over again.

Of course I can put the exception handling code into a function to not duplicate it. However, I still need to write this construct over and over again. Is there a way to handle it more generic? Like:

??? (... code ...);

or

??? { ... code ...};

Where ??? would do the try and re-use the exception handling code everytime? I hope this is understandable.

Have you looked at std.exception.ifThrown? You could define a handler function and pass it to ifThrown for each expression that may throw. If it's a statement, I believe you could wrap it in a lambda which is immediately called, then append .ifThrown(handler).

Reply via email to