我试图很好地捕捉错误,就像我在 Java 中所做的那样。该程序与此类似:
try
{
New-Object System.DirectoryServices.DirectoryEntry($SearchString, $username, $pass)
Write-Ouput "Continue program"
}
catch
{
Write-Output "Some error"
}
永远不会捕获异常。事实上,当一切顺利时,catch 方法就会执行。从我能得到的不同错误来看,看起来并没有引发异常:
format-default : The following exception occurred while retrieving member "distinguishedName": "The server is not operational.
"
+ CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
format-default : The following exception occurred while retrieving member "distinguishedName": "The user name or password is
incorrect.
"
+ CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
如何以更用户友好的方式捕获这些错误?
也许在这种情况下是不可能的。
经过一些测试,我认为问题在于,尽管错误消息说明了什么,但由于某种原因没有引发异常。所以这就是为什么我无法捕捉它。我也尝试过使用getType,close和其他方法,但我得到了同样的错误:
The following exception occurred while retrieving member "GetType": "The user name or password is incorrect.
"
...
这个解决方案似乎也不起作用,同样的行为。
问题相关。