testing for exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cj

    #1

    testing for exceptions

    HttpWebRequest. Create(String) doesn't return any error codes but it does
    show exceptions
    http://msdn.microsoft.com/library/de...eatetopic1.asp
    When I run
    REQUEST = Net.HttpWebRequ est.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
  • Cerebrus99

    #2
    Re: testing for exceptions

    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.Messa ge)
    Finally
    MyRequest.Close ()
    End Try

    Hope this helps,

    Regards,
    Cerebrus.

    "cj" <cj@nospam.nosp am> wrote in message
    news:OFdf$wZMGH A.1424@TK2MSFTN GP12.phx.gbl...[color=blue]
    > HttpWebRequest. Create(String) doesn't return any error codes but it does
    > show exceptions
    >[/color]
    http://msdn.microsoft.com/library/de...us/cpref/html/
    frlrfsystemnetw ebrequestclassc reatetopic1.asp[color=blue]
    > When I run
    > REQUEST = Net.HttpWebRequ est.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[/color]


    Comment

    • Ken Tucker [MVP]

      #3
      Re: testing for exceptions

      Hi,

      If request is nothing then it failed.

      Ken
      --------------
      "cj" <cj@nospam.nosp am> wrote in message
      news:OFdf$wZMGH A.1424@TK2MSFTN GP12.phx.gbl...[color=blue]
      > HttpWebRequest. Create(String) doesn't return any error codes but it does
      > show exceptions
      > http://msdn.microsoft.com/library/de...eatetopic1.asp
      > When I run
      > REQUEST = Net.HttpWebRequ est.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[/color]


      Comment

      • TerryFei

        #4
        RE: testing for exceptions

        Hi Cj,
        Welcome to MSDN Newsgroup!
        [color=blue][color=green]
        >>How do I test to see if it has worked?[/color][/color]
        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


        --------------------[color=blue]
        >Date: Tue, 14 Feb 2006 14:24:17 -0500
        >From: cj <cj@nospam.nosp am>
        >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.14 24@TK2MSFTNGP12 .phx.gbl>
        >Newsgroups: microsoft.publi c.dotnet.langua ges.vb
        >NNTP-Posting-Host: 208.254.170.98
        >Lines: 1
        >Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP12.phx. gbl
        >Xref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.langua ges.vb:317700
        >X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
        >
        >HttpWebRequest .Create(String) doesn't return any error codes but it does
        >show exceptions
        >http://msdn.microsoft.com/library/de...-us/cpref/html[/color]
        /frlrfsystemnetw ebrequestclassc reatetopic1.asp[color=blue]
        >When I run
        >REQUEST = Net.HttpWebRequ est.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
        >[/color]

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: testing for exceptions

          cj,
          In addition to the other comments, I normally use something like:

          Dim uri As New Uri("http://www.tsbradley.n et")
          Dim request As HttpWebRequest = DirectCast(WebR equest.Create(u ri),
          HttpWebRequest)
          Dim response As HttpWebResponse
          Try
          response = DirectCast(requ est.GetResponse (), HttpWebResponse )
          Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse
          response = DirectCast(ex.R esponse, HttpWebResponse )
          End Try

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

          Debug.WriteLine (response.Statu sCode, "statusCode ")

          The:

          Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse

          Will only catch WebExceptions where the WebException.Re sponse 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.nosp am> wrote in message
          news:OFdf$wZMGH A.1424@TK2MSFTN GP12.phx.gbl...
          | HttpWebRequest. Create(String) doesn't return any error codes but it does
          | show exceptions
          |
          http://msdn.microsoft.com/library/de...eatetopic1.asp
          | When I run
          | REQUEST = Net.HttpWebRequ est.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


          Comment

          Working...