为什么我在运行这两个简单样本时会在控制台上显示错误消息?我希望我得到"错误测试:)"打印在控制台上:
Get-WmiObject:RPC服务器不可用.(来自HRESULT的异常:0x800706BA)在行:3 char:15 + Get-WmiObject <<<< --ComputerName possible.nonexisting.domain.com -Credential(Get-Credential)-Class Win32_logicaldisk + CategoryInfo:InvalidOperation:(:) [ Get-WmiObject],COMException + FullyQualifiedErrorId:GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
要么
试图除以零.在行:3 char:13 + $ i = 1/<<<< 0
+ CategoryInfo:NotSpecified:(:) [],ParentContainsErrorRecordException + FullyQualifiedErrorId:RuntimeException
第一个例子:
try
{
$i = 1/0
Write-Host $i
}
catch [Exception]
{
Write-Host "Error testing :)"
}
Run Code Online (Sandbox Code Playgroud)
第二个例子:
try
{
Get-WmiObject -ComputerName possibly.nonexisting.domain.com -Credential (Get-Credential) -Class Win32_logicaldisk
}
catch [Exception]
{
Write-Host "Error testing :)"
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你!