Thread overview | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 16, 2012 scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | >> 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 Re: scope(exit) without exception handling? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | 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.
|
Copyright © 1999-2021 by the D Language Foundation