Jump to page: 1 2 3
Thread overview
scope(exit) without exception handling?
May 16, 2012
Mehrdad
May 16, 2012
Jonathan M Davis
May 16, 2012
Mehrdad
May 16, 2012
Jonathan M Davis
May 16, 2012
Brad Roberts
May 16, 2012
Mehrdad
May 16, 2012
Robert DaSilva
May 16, 2012
Trass3r
May 16, 2012
deadalnix
May 16, 2012
Mehrdad
May 16, 2012
Mehrdad
May 16, 2012
H. S. Teoh
May 16, 2012
Mehrdad
May 16, 2012
H. S. Teoh
May 16, 2012
Timon Gehr
May 16, 2012
H. S. Teoh
May 16, 2012
Artur Skawina
May 16, 2012
Walter Bright
May 16, 2012
Timon Gehr
May 16, 2012
Jonathan M Davis
May 16, 2012
Timon Gehr
May 16, 2012
deadalnix
May 16, 2012
Jacob Carlborg
May 16, 2012
David Nadlinger
May 16, 2012
I'm writing some (low-level) code with no exception handling available whatsoever... either the code runs, or it doesn't.

Is there any way for me to use scope(exit) (or perhaps a destructor, like RAII) to mean, "Execute this block of code for me when the block is exited, will ya?", *without* introducing dependencies on exception handling?
May 16, 2012
On Wednesday, May 16, 2012 05:54:04 Mehrdad wrote:
> I'm writing some (low-level) code with no exception handling available whatsoever... either the code runs, or it doesn't.
> 
> Is there any way for me to use scope(exit) (or perhaps a destructor, like RAII) to mean, "Execute this block of code for me when the block is exited, will ya?", *without* introducing dependencies on exception handling?

scope(exit) stuff;
otherStuff;

is lowered to something like

try
{
    otherStuff;
}
finally
{
    stuff;
}

So, you can use scope(exit) if the above code is acceptable for whatever you're doing. Otherwise, no, you can't.

Destructors should work regardless of what you're doing with exceptions though, so I would expect RAII to work.

- Jonathan M Davis
May 16, 2012
On Wednesday, 16 May 2012 at 05:06:51 UTC, Jonathan M Davis wrote:

> scope(exit) stuff;
> otherStuff;
>
> is lowered to something like
>
> try
> {
>     otherStuff;
> }
> finally
> {
>     stuff;
> }
>
> So, you can use scope(exit) if the above code is acceptable for whatever you're doing. Otherwise, no, you can't.

Thanks, though I already knew that...


> Destructors should work regardless of what you're doing with exceptions though, so I would expect RAII to work.


Well, RAII is pretty much just a finally block...
It seems like all of these emit references _d_local_unwind2 and stuff, so it seems like it's not the way you expect...
May 16, 2012
On 5/15/2012 10:06 PM, Jonathan M Davis wrote:
> On Wednesday, May 16, 2012 05:54:04 Mehrdad wrote:
>> I'm writing some (low-level) code with no exception handling available whatsoever... either the code runs, or it doesn't.
>>
>> Is there any way for me to use scope(exit) (or perhaps a destructor, like RAII) to mean, "Execute this block of code for me when the block is exited, will ya?", *without* introducing dependencies on exception handling?
> 
> scope(exit) stuff;
> otherStuff;
> 
> is lowered to something like
> 
> try
> {
>     otherStuff;
> }
> finally
> {
>     stuff;
> }
> 
> So, you can use scope(exit) if the above code is acceptable for whatever you're doing. Otherwise, no, you can't.
> 
> Destructors should work regardless of what you're doing with exceptions though, so I would expect RAII to work.
> 
> - Jonathan M Davis

And if otherStuff is marked all nothrow, then the exception parts are pulled out.  It's pretty much the entire point of having nothrow annotations.
May 16, 2012
On Wednesday, 16 May 2012 at 05:39:08 UTC, Brad Roberts wrote:
> On 5/15/2012 10:06 PM, Jonathan M Davis wrote:
>> On Wednesday, May 16, 2012 05:54:04 Mehrdad wrote:
>>> I'm writing some (low-level) code with no exception handling
>>> available whatsoever... either the code runs, or it doesn't.
>>>
>>> Is there any way for me to use scope(exit) (or perhaps a
>>> destructor, like RAII) to mean, "Execute this block of code for
>>> me when the block is exited, will ya?", *without* introducing
>>> dependencies on exception handling?
>> 
>> scope(exit) stuff;
>> otherStuff;
>> 
>> is lowered to something like
>> 
>> try
>> {
>>     otherStuff;
>> }
>> finally
>> {
>>     stuff;
>> }
>> 
>> So, you can use scope(exit) if the above code is acceptable for whatever you're doing. Otherwise, no, you can't.
>> 
>> Destructors should work regardless of what you're doing with exceptions though, so I would expect RAII to work.
>> 
>> - Jonathan M Davis
>
> And if otherStuff is marked all nothrow, then the exception parts are pulled out.  It's pretty much the entire point of
> having nothrow annotations.

Oooh, *that* I did not know. Very interesting, thanks for pointing that out!
May 16, 2012
On Wednesday, 16 May 2012 at 05:46:03 UTC, Mehrdad wrote:=
> Oooh, *that* I did not know. Very interesting, thanks for pointing that out!

You could try scope(success), but the nothrow annotations sound like a better idea
May 16, 2012
On Wednesday, May 16, 2012 07:30:44 Mehrdad wrote:
> Well, RAII is pretty much just a finally block...
> It seems like all of these emit references _d_local_unwind2 and
> stuff, so it seems like it's not the way you expect...

It all depends on how it's implemented I guess. RAII doesn't directly have anything to do with exceptions (though it's a good way to write exception-safe code), so it would be perfectly possible to have it in a language with no exceptions at all, but I guess that it could be implemented in a manner similar to finally blocks, much as I wouldn't have expected it. I haven't looked at what exactly the compiler generates though, so if you've dug into that, you know more about it than I do.

- Jonathan M Davis
May 16, 2012
On 5/15/2012 8:54 PM, Mehrdad wrote:
> Is there any way for me to use scope(exit) (or perhaps a destructor, like RAII)
> to mean, "Execute this block of code for me when the block is exited, will ya?",
> *without* introducing dependencies on exception handling?

Make sure the guarded code is 'nothrow', and it should work.
May 16, 2012
>> scope(exit) stuff;
>> otherStuff;
>>
>> is lowered to something like
>>
>> try
>> {
>>     otherStuff;
>> }
>> finally
>> {
>>     stuff;
>> }
>>
> And if otherStuff is marked all nothrow, then the exception parts are pulled out.  It's pretty much the entire point of
> having nothrow annotations.

This should be added to
http://dlang.org/function.html#nothrow-functions
May 16, 2012
Le 16/05/2012 11:59, Trass3r a écrit :
>>> scope(exit) stuff;
>>> otherStuff;
>>>
>>> is lowered to something like
>>>
>>> try
>>> {
>>> otherStuff;
>>> }
>>> finally
>>> {
>>> stuff;
>>> }
>>>
>> And if otherStuff is marked all nothrow, then the exception parts are
>> pulled out. It's pretty much the entire point of
>> having nothrow annotations.
>
> This should be added to
> http://dlang.org/function.html#nothrow-functions

Except for thing throw by the runtime, which can basically occur at any moment.
« First   ‹ Prev
1 2 3