gpt4 book ai didi

Exit the function without aborting the script(退出函数而不中止脚本)

转载 作者:bug小助手 更新时间:2023-10-22 15:10:17 25 4
gpt4 key购买 nike



I have a function that accepts an array of positive values only. Dies when any negative value is given.

我有一个函数,它只接受一个正值数组。当给定任何负值时死亡。


# Consider edge cases
# First check if all elements are > 0, if not throw informative error
# Then if any of element is zero return 0
# Only else proceed further towards calculation


sub geometric_mean(@data) {
if @data.any < 0 {die "Not possible to calculate geometric mean for negative values"};
if @data.any == 0 { return 0 };
my $sum;
for @data {
$sum += log($_);
}
return exp($sum/@data.elems)
}

With this if I try to run the script:

如果我尝试运行脚本,请执行以下操作:


my @data = [1, 2, 3.8, -1];
say geometric_mean(@data);

say "Hello world";

the code exits at the function call without running the last line i.e does not run the codes down the function:

代码在函数调用处退出而不运行最后一行,即不运行函数中的代码:


Not possible to calculate geometric mean for negative values
in method throw at 'SETTING::'src/core.c/Exception.rakumod line 65
in sub die at 'SETTING::'src/core.c/control.rakumod line 255
in sub geometric_mean at c:\Users\suman\sum.p6 line 8
in block <unit> at c:\Users\suman\sum.p6 line 20

The modification:

修改内容:


sub geometric_mean(@data) {
if @data.any < 0 {die "Not possible to calculate geometric mean for negative values"};
CATCH { default { warn " Not possible to calculate geometric mean for negative values"; } };
if @data.any == 0 { return 0 };
my $sum;
for @data {
$sum += log($_);
}
return exp($sum/@data.elems)
}

now runs the last line too with output:

现在也运行最后一行输出:


    Not possible to calculate geometric mean for negative values
Nil
in sub warn at 'SETTING::'src/core.c/control.rakumod line 269
Hello world

How to handle such cases?
I looked at try CATCH blocks but that would execute the code below those blocks within the function which I want to avoid.

如何处理此类案件?我看了try CATCH块,但它会在函数中执行那些块下面的代码,我想避免这些块。


I went through this:

我经历了这个:



  1. Succinct way to change thrown Exception to Failure?

  2. https://docs.raku.org/routine/throw

  3. https://docs.raku.org/type/Exception

  4. https://dev.to/lizmat/exceptions-3g0i


更多回答

Usually the catch is where you call the function, not in it.

通常,catch是调用函数的地方,而不是函数中的catch。

Why not change the die to a fail? fail "Not possible to calculate geometric mean for negative values" if @data.any < 0

为什么不把死亡变成失败呢?fail“无法计算负值的几何平均值”,如果@data.any<0

优秀答案推荐

die is for fatal exceptions. If you want to continue control flow, you don't want a fatal exception.

死亡是致命的例外。如果您想继续控制流,您不希望出现致命的异常。


There are two ways you could fix this. First, use fail instead of die, and then check if the call failed with a with:

有两种方法可以解决这个问题。首先,使用fail而不是die,然后使用带有的检查调用是否失败:


sub geometric_mean(@data) {
if @data.any < 0 {fail "Not possible to calculate geometric mean for negative values"};
// etc
}

my @data = [1, 2, 3.8, -1];
with geometric_mean(@data) -> {} else {say .exception.message};

say "Hello world";

The other way is to not throw an exception at all, and instead return:

另一种方法是根本不抛出异常,而是返回:


sub geometric_mean(@data) {
if @data.any < 0 {say "Not possible to calculate geometric mean for negative values"; return};
// etc
}

my @data = [1, 2, 3.8, -1];
geometric_mean(@data);

say "Hello world";


I think you are asking "how can I avoid the backtrace details emitted on a fail or die??

我想你在问“我如何才能避免失败或死亡时发出的回溯细节?”??


If so, you can just not generate an exception, like this:

如果是这样,您就不能生成异常,如下所示:


sub geometric_mean(@data) {
if @data.any < 0 { return "Not possible to calculate geometric mean for negative values" }
if @data.any == 0 { return 0 };
my $sum;
for @data {
$sum += log($_);
}
exp($sum/@data.elems)
}

my @data = [1, 2, 3.8, -1];
say geometric_mean(@data);

say "Hello world";

Not possible to calculate geometric mean for negative values
Hello world

EDIT: removed my speculative speculative try/CATCH variant since per comment not good to use try and CATCH in the same block.

EDIT:删除了我的推测性try/CATCH变体,因为每个注释在同一块中使用try和CATCH是不好的。


更多回答

Note you shouldn't use try if you intend on using CATCH.

请注意,如果您打算使用CATCH,则不应使用try。

@RawleyFowler try is useful to keep the scope of the CATCH contained to what it needs to be. If they got rid of the try then the CATCH would prevent e.g. typoed method names from throwing an error

@RawleyFowler try有助于将CATCH的范围保持在需要的范围内。如果他们取消了try,那么CATCH将防止例如键入错误的方法名引发错误

although it also occurs to me that just creating a new scope with { } is probably a better idea since it would not affect a typoed method name inside of the CATCH

尽管我也想到用{}创建一个新的作用域可能是一个更好的主意,因为它不会影响CATCH中键入的方法名

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com