gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-24 23:02:34 45 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:

现在也用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.

通常,问题是在调用函数的地方,而不是在函数中。

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

为什么不把骰子改成失败呢?如果@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.

编辑:删除了我的推测性的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

@Rawley Fowler Try有助于将捕获物的范围保持在需要的范围内。如果它们去掉了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中输入错误的方法名称

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