4

I have a problem with catching exception from NewWebServiceProxy cmdlet

try {
    $myService = New-WebServiceProxy -Uri "http://localhost/someservice.svc"
}
catch {
    Write-Log ([string]::Format("Error : {0}", $_.Exception.Message))
}

When I run it i get this unhandled exception : New-WebServiceProxy :

The request failed with HTTP status 404: Not Found. At 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

Somebody could ask me why try catch do not catch this exception ? Thanks for any aswer

Zabaa
  • 327
  • 1
  • 4
  • 16

2 Answers2

7

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.

Mitul
  • 9,734
  • 4
  • 43
  • 60
  • Thanks it work, but the exception that I catch now is diffrent then exception above (HTTP status 404). Actual catched exception : `There was an error downloading 'http://localhost/someservice.svc'`. Can I catch the 404 exception ? – Zabaa Sep 03 '13 at 14:25
  • 1
    I think you want to catch [System.Net.WebException]. – Keith Hill Sep 03 '13 at 14:32
  • Yes, but even when i add `catch [System.Net.WebException]` ps don't catch it. – Zabaa Sep 03 '13 at 14:35
  • I try call a request `try { $request = [System.Net.WebRequest]::Create("http://localhost/someservice.svc") $response = $request.GetResponse() } catch { Write-Error ([string]::Format("Response from request : {0}", $_.Exception.Message)) }` And catch give me this : `Exception c alling "GetResponse" with "0" argument(s): "The remote server returned an error: (404) Not Found." ` – Zabaa Sep 03 '13 at 14:53
  • 1
    So if you are doing System.Net.WebRequest then you need to check this on catching 404. http://stackoverflow.com/questions/9090849/random-webrequest-results-with-powershell – Mitul Sep 03 '13 at 15:23
0

What helped me was doing something more like

try{
    New-WebServiceProxy -URI $webURL -ErrorAction Stop
}
catch [System.Net.WebException] {
        return [PSCustomObject]@{
            SOAPStatus = 'Failed'
            SOAPException = $_.Exception.Message
            SOAPInnerException = $_.Exception.InnerException.Message
        } 
    }
    catch {
        return [PSCustomObject]@{
            SOAPStatus = 'Failed'
            SOAPError = $_.Exception.Message
        } 
    }

By catching the inner exception I found I would get the web response as well if there was an issue with it. Which usually in these cases there was something like this for output:

SOAPStatus : Failed SOAPException : There was an error downloading 'http://webserviceurl.com:1010/SystemServiceQuery'.

SOAPInnerException : The request failed with HTTP status 401: Unauthorized.

Of course you could then capture what type of innerexception and give more useful feedback to your users and such.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ron Zimmer
  • 23
  • 5