我正在使用 power shell 控制台运行以下 power shell 脚本:
$ob1 = "nonexistingclass"
$a = new-object $ob1
Write-Output "Created new object"
Write-Output "End"
Run Code Online (Sandbox Code Playgroud)
这会打印错误。然后继续打印“Created new object”和“End”。所以我假设这是一个非终止错误。
但是,如果我将 try catch 块放在新对象周围,如下所示:
$ob1 = "nonexistingclass"
try
{
$a = new-object $ob1
Write-Output "Created new object"
}
catch
{
Write-Error "Exception Message: $($_.Exception.Message)"
}
Write-Output "End"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,catch 块被命中并写入异常消息。
我的问题是:
| 归档时间: |
|
| 查看次数: |
3086 次 |
| 最近记录: |