![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest
Posts: n/a
| Trapping exceptions Hi, I'm just getting into PowerShell and I'm trying to create a simple script which goes onto the web and drags back some content that it processes further. What I'm stuggling to do is cope with the exceptions. I have some code: $baseUrl = 'http://www.epguides.com/'; $contents = (new-object System.Net.WebClient).DownloadString( $baseUrl + $title ); $return = [string] ''; trap [System.Net.WebException] { $return = "Could not find programme " + $title; return $return; } But any time run with an illegal title (so it 404's), I get error output - despite wanting to cope with it above in a simple message. Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (404) Not Found." At C:\bin\Get-EpisodeList.ps1:4 char:61 + $contents = (new-object System.Net.WebClient).DownloadString( <<<< $baseUrl + $title ); Could not find programme 25 What is the correct way of suppressing this? Additionally, if I ever make this work as a filter, how should I terminate it then? Regards, Ray |
| | #2 (permalink) |
| Guest
Posts: n/a
| RE: Trapping exceptions You should trap MethodInvocationException, WebException is the inner exception. PS> $error[0].exception Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (404) Not Found." PS> $error[0].exception.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True MethodInvocationException System.Management.Automation.MethodException -- PS> $error[0].exception.innerexception.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True WebException System.InvalidOperationException Wei Wu [MSFT] Windows PowerShell Team Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Ray Hayes" wrote: > Hi, > > I'm just getting into PowerShell and I'm trying to create a simple script > which goes onto the web and drags back some content that it processes further. > > What I'm stuggling to do is cope with the exceptions. > > I have some code: > > $baseUrl = 'http://www.epguides.com/'; > $contents = (new-object System.Net.WebClient).DownloadString( $baseUrl + > $title ); > $return = [string] ''; > > trap [System.Net.WebException] > { > $return = "Could not find programme " + $title; > return $return; > } > > But any time run with an illegal title (so it 404's), I get error output - > despite wanting to cope with it above in a simple message. > > Exception calling "DownloadString" with "1" argument(s): "The remote server > returned an error: (404) Not Found." > At C:\bin\Get-EpisodeList.ps1:4 char:61 > + $contents = (new-object System.Net.WebClient).DownloadString( <<<< > $baseUrl + $title ); > Could not find programme 25 > > What is the correct way of suppressing this? > > Additionally, if I ever make this work as a filter, how should I terminate > it then? > > > Regards, > > Ray |
| | #3 (permalink) |
| Guest
Posts: n/a
| RE: Trapping exceptions Ok, I'm trapping MethodInvocationException now. But the console still gets the exception shown, how should I suppress this? I've already delt with it! Regards, Ray "Wei Wu [MSFT]" wrote: > You should trap MethodInvocationException, WebException is the inner exception. > > PS> $error[0].exception > Exception calling "DownloadString" with "1" argument(s): "The remote server > returned an error: (404) Not Found." > PS> $error[0].exception.gettype() > > IsPublic IsSerial Name BaseType > -------- -------- ---- -------- > True True MethodInvocationException > System.Management.Automation.MethodException > -- > PS> $error[0].exception.innerexception.gettype() > > IsPublic IsSerial Name BaseType > -------- -------- ---- -------- > True True WebException > System.InvalidOperationException > Wei Wu [MSFT] > Windows PowerShell Team > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no rights. > > > > "Ray Hayes" wrote: > > > Hi, > > > > I'm just getting into PowerShell and I'm trying to create a simple script > > which goes onto the web and drags back some content that it processes further. > > > > What I'm stuggling to do is cope with the exceptions. > > > > I have some code: > > > > $baseUrl = 'http://www.epguides.com/'; > > $contents = (new-object System.Net.WebClient).DownloadString( $baseUrl + > > $title ); > > $return = [string] ''; > > > > trap [System.Net.WebException] > > { > > $return = "Could not find programme " + $title; > > return $return; > > } > > > > But any time run with an illegal title (so it 404's), I get error output - > > despite wanting to cope with it above in a simple message. > > > > Exception calling "DownloadString" with "1" argument(s): "The remote server > > returned an error: (404) Not Found." > > At C:\bin\Get-EpisodeList.ps1:4 char:61 > > + $contents = (new-object System.Net.WebClient).DownloadString( <<<< > > $baseUrl + $title ); > > Could not find programme 25 > > > > What is the correct way of suppressing this? > > > > Additionally, if I ever make this work as a filter, how should I terminate > > it then? > > > > > > Regards, > > > > Ray |
| | #4 (permalink) |
| Guest
Posts: n/a
| RE: Trapping exceptions Use "continue" instead of "return" in your trap block if you don't want to see this exception. -- Wei Wu [MSFT] Windows PowerShell Team Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Ray Hayes" wrote: > Ok, I'm trapping MethodInvocationException now. But the console still gets > the exception shown, how should I suppress this? I've already delt with it! > > Regards, > > Ray > > "Wei Wu [MSFT]" wrote: > > > You should trap MethodInvocationException, WebException is the inner exception. > > > > PS> $error[0].exception > > Exception calling "DownloadString" with "1" argument(s): "The remote server > > returned an error: (404) Not Found." > > PS> $error[0].exception.gettype() > > > > IsPublic IsSerial Name BaseType > > -------- -------- ---- -------- > > True True MethodInvocationException > > System.Management.Automation.MethodException > > -- > > PS> $error[0].exception.innerexception.gettype() > > > > IsPublic IsSerial Name BaseType > > -------- -------- ---- -------- > > True True WebException > > System.InvalidOperationException > > Wei Wu [MSFT] > > Windows PowerShell Team > > Microsoft Corporation > > This posting is provided "AS IS" with no warranties, and confers no rights. > > > > > > > > "Ray Hayes" wrote: > > > > > Hi, > > > > > > I'm just getting into PowerShell and I'm trying to create a simple script > > > which goes onto the web and drags back some content that it processes further. > > > > > > What I'm stuggling to do is cope with the exceptions. > > > > > > I have some code: > > > > > > $baseUrl = 'http://www.epguides.com/'; > > > $contents = (new-object System.Net.WebClient).DownloadString( $baseUrl + > > > $title ); > > > $return = [string] ''; > > > > > > trap [System.Net.WebException] > > > { > > > $return = "Could not find programme " + $title; > > > return $return; > > > } > > > > > > But any time run with an illegal title (so it 404's), I get error output - > > > despite wanting to cope with it above in a simple message. > > > > > > Exception calling "DownloadString" with "1" argument(s): "The remote server > > > returned an error: (404) Not Found." > > > At C:\bin\Get-EpisodeList.ps1:4 char:61 > > > + $contents = (new-object System.Net.WebClient).DownloadString( <<<< > > > $baseUrl + $title ); > > > Could not find programme 25 > > > > > > What is the correct way of suppressing this? > > > > > > Additionally, if I ever make this work as a filter, how should I terminate > > > it then? > > > > > > > > > Regards, > > > > > > Ray |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another error trapping problem | Rob Campbell | PowerShell | 2 | 01-25-2008 07:06 PM |
| Question on Exception Trapping | Brandon Shell | PowerShell | 6 | 09-07-2007 02:20 PM |
| Help w/ error trapping | Blip | PowerShell | 3 | 02-12-2007 07:34 PM |
| Trapping and ADSI? | Dan Metzler | PowerShell | 19 | 10-09-2006 01:12 PM |
| trapping oddity | klumsy@xtra.co.nz | PowerShell | 2 | 09-29-2006 03:24 PM |