Trailing catch on function?
André Puel via Digitalmars-d
digitalmars-d at puremagic.com
Sun Nov 13 11:05:27 PST 2016
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.
More information about the Digitalmars-d
mailing list