Powershell don't catch exception from NewWebServiceProxy Powershell don't catch exception from NewWebServiceProxy powershell powershell

Powershell don't catch exception from NewWebServiceProxy


This is one of the most common issues while catching an unhandled exception in powershell. You need to use -ErrorAction Stop in the command 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))}

Updated: To catch Http exception include [System.Net.WebException] as noted by Keith Hill in the comment below.