November 08, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Tuesday, 8 November 2016 at 11:24:59 UTC, Anonymouse wrote:
> You can eat the exception by returning in the scope guard. Since it seems to trigger on Throwables it also eats Errors, sadly.
>
Interesting!
Considering this works:
scope(failure) { abort(); return; }
I had expected this to work also:
scope(failure) abort();
But I guess D lacks support for noreturn attribute on abort?
|
November 09, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments:
| On 8 November 2016 at 09:37, Walter Bright via Digitalmars-d < digitalmars-d@puremagic.com> wrote:
> On 11/6/2016 8:30 PM, Manu via Digitalmars-d wrote:
>
>> Hey people, I'm passing lots of D function pointers to C, and naturally,
>> the C
>> api expects the fp signatures are all nothrow.
>>
>> Which means, my D functions all look like this:
>>
>> void callback() nothrow
>> {
>> try
>> {
>> ...lots of code...
>> }
>> catch (Exception e)
>> {
>> ...log error or abort...
>> }
>> }
>>
>>
>> I'm generally annoyed by all the extra indentation.
>>
>
> void callback() nothrow
> {
> scope (failure)
> {
> ...log error or abort...
> }
> ...lots of code...
> }
>
scope(failure) doesn't catch... how is that function nothrow?
|
November 09, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker Attachments:
| On 8 November 2016 at 12:50, Mike Parker via Digitalmars-d < digitalmars-d@puremagic.com> wrote:
> On Tuesday, 8 November 2016 at 01:50:26 UTC, Walter Bright wrote:
>
>> On 11/7/2016 4:12 PM, Robert burner Schadek wrote:
>>
>>> On Monday, 7 November 2016 at 23:37:18 UTC, Walter Bright wrote:
>>>
>>>> void callback() nothrow
>>>> {
>>>> scope (failure)
>>>> {
>>>> ...log error or abort...
>>>> }
>>>> ...lots of code...
>>>> }
>>>>
>>>
>>> Who to get the Exception thrown in the scope(failure)
>>>
>>
>> You don't. The exception is also rethrown, so it isn't an exact replacement. (The 'nothrow' is a mistake on my part.)
>>
>
> In this specific case, a function used as a callback for a C API, it really ought to be nothrow because of the inconsistent behavior with propagating exceptions across language boundaries. scope(exit) just isn't useful here.
>
It's not strictly useful for interaction with C, it would be equally useful in the situation where a nothrow function returns an error code or something. The tail-catch block could do the translation and return the error, and it would be in a nice place separate from the main function body.
|
November 09, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Tuesday, 8 November 2016 at 16:02:25 UTC, Manu wrote:
> scope(failure) doesn't catch... how is that function nothrow?
Seems like you overlooked Anonymouse's comment? scope(failure) catches just fine.
scope(failure) return -1;
throw new Exception("failure");
return 0;
This will return -1, with no need for extra indentation.
|
November 12, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel N | On Wednesday, 9 November 2016 at 09:49:08 UTC, Daniel N wrote:
> On Tuesday, 8 November 2016 at 16:02:25 UTC, Manu wrote:
>> scope(failure) doesn't catch... how is that function nothrow?
>
> Seems like you overlooked Anonymouse's comment? scope(failure) catches just fine.
>
> scope(failure) return -1;
> throw new Exception("failure");
> return 0;
>
> This will return -1, with no need for extra indentation.
I understand its convenience, but as long as it also catches Errors I'm not sure this is something you want to do though. You won't be able to differentiate between invalid input and bugs in the code, potentially continuing in an invalid state.
Subsetting failure into a scope(exception) would work, but I'm not sure how the interplay between them would work. Or are Errors so fatal that it should always immediately crash the program, ignored by scope(failure)?
|
November 13, 2016 Re: Trailing catch on function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Saturday, 12 November 2016 at 12:17:35 UTC, Anonymouse wrote:
> On Wednesday, 9 November 2016 at 09:49:08 UTC, Daniel N wrote:
>> On Tuesday, 8 November 2016 at 16:02:25 UTC, Manu wrote:
>>> scope(failure) doesn't catch... how is that function nothrow?
>>
>> Seems like you overlooked Anonymouse's comment? scope(failure) catches just fine.
>>
>> scope(failure) return -1;
>> throw new Exception("failure");
>> return 0;
>>
>> This will return -1, with no need for extra indentation.
>
> I understand its convenience, but as long as it also catches Errors I'm not sure this is something you want to do though. You won't be able to differentiate between invalid input and bugs in the code, potentially continuing in an invalid state.
>
> Subsetting failure into a scope(exception) would work, but I'm not sure how the interplay between them would work. Or are Errors so fatal that it should always immediately crash the program, ignored by scope(failure)?
That is how I do it:
auto csafe(alias f)() nothrow {
try {
return f();
} catch(Throwable e) {
import core.stdc.stdlib : abort;
import std.stdio : stderr;
stderr.writeln("Fatal error.");
stderr.writeln(e);
abort();
assert(false);
}
}
And then:
extern (C) int yada() nothrow {
return csafe!(() {
return maythrowFunction();
});
}
You might extend the `csafe` function to do some kind of error handling, or invoking error callback instead of just aborting the execution.
|
Copyright © 1999-2021 by the D Language Foundation