<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 4 August 2014 17:13, Jacob Carlborg via Digitalmars-d <span dir="ltr"><<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">On Sunday, 3 August 2014 at 15:28:43 UTC, Manu via Digitalmars-d wrote:<br>

</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="">
I'm trying to make better use of scope guards, but I find myself belting<br>
out try/catch statements almost everywhere.<br>
I'm rather disappointed, because scope guards are advertised to offer the<br>
promise of eliminating try/catch junk throughout your code, and I'm just<br>
not finding that to be the practical reality.<br>
<br>
I think the core of the problem is that scope(failure) is indiscriminate,<br>
but I want to filter it for particular exceptions. The other issue is that<br>
you still need a catch() if you don't actually want the program to<br>
terminate, which implies a try... :/<br>
<br></div><div class="">
One thing that may be leveraged to eliminate most catch blocks is the<br>
existing ability to return from scope guard blocks, allowing to gracefully<br>
return from a function while unwinding, akin to a catch.<br>
The problem then is that you can't handle specific exceptions.<br>
<br>
I'm thinking this would make all the difference:<br>
<br>
scope(failure, MyException e) // only executed for exceptions of type<br>
MyException<br>
{<br>
  writeln(e.msg); // can refer to the exception in this failure block<br>
  return failureValue; // and can gracefully return from the function too<br>
}<br>
<br>
That would eliminate about 80% of my try/catch blocks.<br>
</div></blockquote>
<br>
That is exactly like a catch-block.</blockquote><div><br></div><div>Yes, but there's no try block, and it is sequenced properly along with any other scheduled scope events.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
The remaining suffer from the problem where I want to respond to exceptions<br>
NOT of a specific type, ie, clean up in the case of an unexpected/unknown<br>
exception.<br>
<br>
scope(failure, ~MyException)<br>
{<br>
  // clean up, because some unexpected exception occurred that I<br>
don't/can't handle.<br>
}<br>
</blockquote>
<br></div>
That I can agree would be useful, but within a catch-block instead. You can always manually check the type of the exception.<br>
<br>
catch (Exception e)<br>
{<br>
    if (auto myException = cast(MyException) e)<br>
        // handle<br>
    else<br>
        // cleanup<br>
}<br></blockquote><div><br></div><div>catch can't appear on its own though... or are you suggesting that it should be possible?</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

Of course this logic could be nicely handled with AST macros ;)<div class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Is there already some mechanism to do this? I couldn't find anything in the<br>
docs.<br>
It seems like an obvious thing to want to do.<br>
</blockquote>
<br></div>
It seems like you want to have a catch-block without a try-block. I think that would be useful. A catch-block without a try-block would implicitly add a try-block to the same scope as the catch-block or to the closest function body. Ruby has this and it's quite nice.<br>
</blockquote><div><br></div><div>I was actually going to suggest that, except I deleted that part of my email thinking I was already getting too elaborate ;)</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

void foo ()<br>
{<br>
    connectToDatabase();<br>
<br>
    catch (DatabaseException e)<br>
        cleanup();<br>
}<br>
</blockquote></div><br></div><div class="gmail_extra">Yeah, that. Except I think scope(failure, Exception e) also has a place; there are instances where you don't want to stop the unwind, but you do want to be able to retrieve some information from the exception during the unwind... useful for error recording or context sensitive recovery.</div>
</div>