Zab*_*baa 4 powershell web-services exception-handling exception new-webserviceproxy
我从NewWebServiceProxy cmdlet捕获异常时遇到问题
try {
$myService = New-WebServiceProxy -Uri "http://localhost/someservice.svc"
}
catch {
Write-Log ([string]::Format("Error : {0}", $_.Exception.Message))
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到了这个未处理的异常:New-WebServiceProxy:
请求失败,HTTP状态为404:未找到.在C:\ Users\SomeUser\AppData\Local\Temp\d052b604-38ad-4827-b952-4ebc66e79c69.ps1:2 char:18 + $ myService = New-WebServiceProxy -Uri
"http://localhost/someservice.svc"+ ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:ObjectNotFound :(http://localhost/someservice.svc:Uri)[New-WebServiceProxy],WebExc eption + FullyQualifiedErrorId:WebException,Microsoft.PowerShell.Commands.NewWebServiceProxy
有人可能会问我为什么尝试捕获不捕获此异常?谢谢你的任何aswer
这是在PowerShell中捕获未处理的异常时最常见的问题之一.您需要-ErrorAction Stop在命令中使用New-WebServiceProxy
try {
$myService = New-WebServiceProxy -Uri "http://localhost/someservice.svc" -ErrorAction Stop
}
catch [System.Net.WebException]{
Write-Log ([string]::Format("Error : {0}", $_.Exception.Message))
}
Run Code Online (Sandbox Code Playgroud)
更新:为了赶上HTTP异常包括[System.Net.WebException]如指出基思·希尔在下面的评论.
| 归档时间: |
|
| 查看次数: |
4214 次 |
| 最近记录: |