nothrow and std.exception.ifThrown

Imperatorn johan_forsberg_86 at hotmail.com
Thu Apr 29 16:20:13 UTC 2021


On Thursday, 29 April 2021 at 16:02:20 UTC, novice2 wrote:
> Hello.
> I need use std.format.format() in nothrow function.
> format() can throw.
> For this case i have special default string.
> I don't want embrace format into try..catch block,
> and i found elegant std.exception.ifThrown.
> But DMD say "ifThrown not nothrow"
>
> https://run.dlang.io/is/kXtt5q
> ```d
> nothrow string foo(int x, string def) {
>     import std.format: format;
>     import std.exception: ifThrown;
>
>     return format("%d", x).ifThrown(def);
> }
>
> Error: function std.exception.ifThrown!(Exception, string, 
> string).ifThrown is not nothrow
> ```
>
> What i can use instead of ifThrown, or how it can be changed to 
> nothrow?
> Thanks.

Try adding scope(failure) assert(0); in foo


More information about the Digitalmars-d-learn mailing list