Home All Groups Group Topic Archive Search About

testing for exceptions

Author
14 Feb 2006 7:24 PM
cj
HttpWebRequest.Create(String) doesn't return any error codes but it does
show exceptions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetwebrequestclasscreatetopic1.asp
When I run
REQUEST = Net.HttpWebRequest.Create(URI)
How do I test to see if it has worked?  And if it didn't which of these
exceptions occured.  I'd hope I don't have to do and if for each one
just in case.

Thanks

Author
14 Feb 2006 9:51 PM
Cerebrus99
Hi,

Why not try the generic Exception class.
Use it's properties to give you more information about the error. As in:

Try
  ' your code here
Catch ex as Exception
    Msgbox(ex.Message)
Finally
    MyRequest.Close()
End Try

Hope this helps,

Regards,
Cerebrus.

"cj" <cj@nospam.nospam> wrote in message
news:OFdf$wZMGHA.1424@TK2MSFTNGP12.phx.gbl...
> HttpWebRequest.Create(String) doesn't return any error codes but it does
> show exceptions
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemnetwebrequestclasscreatetopic1.asp
Show quoteHide quote
> When I run
> REQUEST = Net.HttpWebRequest.Create(URI)
> How do I test to see if it has worked?  And if it didn't which of these
> exceptions occured.  I'd hope I don't have to do and if for each one
> just in case.
>
> Thanks
Author
15 Feb 2006 3:14 AM
Ken Tucker [MVP]
Hi,

        If request is nothing then it failed.

Ken
--------------
Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:OFdf$wZMGHA.1424@TK2MSFTNGP12.phx.gbl...
> HttpWebRequest.Create(String) doesn't return any error codes but it does
> show exceptions
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetwebrequestclasscreatetopic1.asp
> When I run
> REQUEST = Net.HttpWebRequest.Create(URI)
> How do I test to see if it has worked?  And if it didn't which of these
> exceptions occured.  I'd hope I don't have to do and if for each one just
> in case.
>
> Thanks
Author
15 Feb 2006 8:17 AM
TerryFei
Hi Cj,
Welcome to MSDN Newsgroup!

>>How do I test to see if it has worked? 
I agree with Cerebrus's point. We can use try/catch statement to catch
exception and get more information why it failed.
Have a nice day!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security


--------------------
Show quoteHide quote
>Date: Tue, 14 Feb 2006 14:24:17 -0500
>From: cj <cj@nospam.nospam>
>User-Agent: Thunderbird 1.5 (Windows/20051201)
>MIME-Version: 1.0
>Subject: testing for exceptions
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>Content-Transfer-Encoding: 7bit
>Message-ID: <OFdf$wZMGHA.1***@TK2MSFTNGP12.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: 208.254.170.98
>Lines: 1        
>Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317700
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb
>
>HttpWebRequest.Create(String) doesn't return any error codes but it does
>show exceptions
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemnetwebrequestclasscreatetopic1.asp
>When I run
>REQUEST = Net.HttpWebRequest.Create(URI)
>How do I test to see if it has worked?  And if it didn't which of these
>exceptions occured.  I'd hope I don't have to do and if for each one
>just in case.
>
>Thanks
>
Author
21 Feb 2006 3:52 AM
Jay B. Harlow [MVP - Outlook]
cj,
In addition to the other comments, I normally use something like:

        Dim uri As New Uri("http://www.tsbradley.net")
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri),
HttpWebRequest)
        Dim response As HttpWebResponse
        Try
            response = DirectCast(request.GetResponse(), HttpWebResponse)
        Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse
            response = DirectCast(ex.Response, HttpWebResponse)
        End Try

Then I am able to use response.StatusCode to see what the HTTP status
returned was:

        Debug.WriteLine(response.StatusCode, "statusCode")

The:

        Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse

Will only catch WebExceptions where the WebException.Response is of type
HttpWebResponse, other exceptions will continue up to a higher exception
handler...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"cj" <cj@nospam.nospam> wrote in message
news:OFdf$wZMGHA.1424@TK2MSFTNGP12.phx.gbl...
| HttpWebRequest.Create(String) doesn't return any error codes but it does
| show exceptions
|
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetwebrequestclasscreatetopic1.asp
Show quoteHide quote
| When I run
| REQUEST = Net.HttpWebRequest.Create(URI)
| How do I test to see if it has worked?  And if it didn't which of these
| exceptions occured.  I'd hope I don't have to do and if for each one
| just in case.
|
| Thanks