<div dir="ltr">I'm trying to make better use of scope guards, but I find myself belting out try/catch statements almost everywhere.<div>I'm rather disappointed, because scope guards are advertised to offer the promise of eliminating try/catch junk throughout your code, and I'm just not finding that to be the practical reality.</div>
<div><br></div><div>I think the core of the problem is that scope(failure) is indiscriminate, but I want to filter it for particular exceptions. The other issue is that you still need a catch() if you don't actually want the program to terminate, which implies a try... :/</div>
<div><br></div><div>One thing that may be leveraged to eliminate most catch blocks is the existing ability to return from scope guard blocks, allowing to gracefully return from a function while unwinding, akin to a catch.</div>
<div>The problem then is that you can't handle specific exceptions.</div><div><br></div><div>I'm thinking this would make all the difference:</div><div><br></div><div>scope(failure, MyException e) // only executed for exceptions of type MyException</div>
<div>{</div><div>  writeln(e.msg); // can refer to the exception in this failure block</div><div>  return failureValue; // and can gracefully return from the function too<br></div><div>}<br></div><div><br></div><div>That would eliminate about 80% of my try/catch blocks.</div>
<div>The remaining suffer from the problem where I want to respond to exceptions NOT of a specific type, ie, clean up in the case of an unexpected/unknown exception.</div><div><br></div><div>scope(failure, ~MyException)</div>
<div>{</div><div>  // clean up, because some unexpected exception occurred that I don't/can't handle.</div><div>}</div><div><br></div><div><br></div><div>Is there already some mechanism to do this? I couldn't find anything in the docs.</div>
<div>It seems like an obvious thing to want to do.</div></div>