小编jgr*_*012的帖子

如果反引号不能执行,STDERR重定向到STDOUT会丢失

我发现STDERR如果命令执行失败,反引号内的重定向可能会丢失.我对我所看到的行为感到困惑.

$ perl -e 'use strict; use warnings; my $out=`DNE`; print $out'  
Can't exec "DNE": No such file or directory at -e line 1.
Use of uninitialized value in print at -e line 1.

$ perl -e 'use strict; use warnings; my $out=`DNE 2>&1`; print $out'
Use of uninitialized value in print at -e line 1.

$ perl -e 'use strict; use warnings; my $out=`echo 123; DNE 2>&1`; print $out'
123
sh: DNE: command not found …

linux perl

5
推荐指数
2
解决办法
3683
查看次数

如何在异常之前捕获返回

是否有可能捕获由函数创建的输出,这也会导致异常?

function functionWhichCreatesOutputThenCausesAnException() {
    "hello"
    1/0
    "world"
}

try { 
    $result = functionWhichCreatesOutputThenCausesAnException 
} catch {
    $($error[0])
}
Run Code Online (Sandbox Code Playgroud)

我的功能创建的输出显示在我的终端中.我想捕捉"你好".这可能吗?

powershell exception return-value

2
推荐指数
1
解决办法
42
查看次数

标签 统计

exception ×1

linux ×1

perl ×1

powershell ×1

return-value ×1