Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Trapping exceptions

Closed Thread
 
Thread Tools Display Modes
Old 09-11-2006   #1 (permalink)
=?Utf-8?B?UmF5IEhheWVz?=
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
 
Old 09-11-2006   #2 (permalink)
=?Utf-8?B?V2VpIFd1IFtNU0ZUXQ==?=
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

 
Old 09-11-2006   #3 (permalink)
=?Utf-8?B?UmF5IEhheWVz?=
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

 
Old 09-11-2006   #4 (permalink)
=?Utf-8?B?V2VpIFd1IFtNU0ZUXQ==?=
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

 
 
Closed Thread

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








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49