Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 如何在异常之前捕获返回_Exception_Powershell_Return Value - Fatal编程技术网

Exception 如何在异常之前捕获返回

Exception 如何在异常之前捕获返回,exception,powershell,return-value,Exception,Powershell,Return Value,是否可以捕获也会导致异常的函数创建的输出 function functionWhichCreatesOutputThenCausesAnException() { "hello" 1/0 "world" } try { $result = functionWhichCreatesOutputThenCausesAnException } catch { $($error[0]) } 由my函数创建的输出显示在my terminal中。我想捕捉“你好

是否可以捕获也会导致异常的函数创建的输出

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

try { 
    $result = functionWhichCreatesOutputThenCausesAnException 
} catch {
    $($error[0])
}
由my函数创建的输出显示在my terminal中。我想捕捉“你好”。这可能吗

这似乎有效:

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

try { 
    $result = @()
    functionWhichCreatesOutputThenCausesAnException | foreach {$result += $_}
} catch {
    $($error[0])
}

对我也有用。FWIW,它在执行代码块(而不仅仅是调用函数)时也可以工作。谢谢