Results 1 to 5 of 5

Thread: [RESOLVED] Try Catch not Working?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Resolved [RESOLVED] Try Catch not Working?

    I have this code

    Code:
          Try
                Dim myWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
                myWebRequest.Method = "GET"
                myWebRequest.Timeout = (My.Settings.timeout) * 1000
    
                Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)
                Dim myWebSource As New StreamReader(myWebResponse.GetResponseStream())
    
                myPageSource = myWebSource.ReadToEnd()
                myWebResponse.Close()
                myWebSource.Close()
                myWebRequest.Abort()
    
                Return myPageSource
    
            Catch ex As Exception
                Return ""
            End Try
    If there is an error i would expect it to not throw an error but just return a ""

    But when in debug sometimes I get an error thrown at this line:
    Code:
                Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)
    And it shows the dialog box. Is this because its in debug mode. Im quite sure this is not true. So why is it showing the error even the whole code is in a Try Catch loop

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Try Catch not Working?

    It does not matter if you step thu the code via a break point, when the exception is raised the catch part of the try kicks in so this seems wrong that an exception is propagated to the UI instead of the catch.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Try Catch not Working?

    is it to do with the "directcast" bit?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Try Catch not Working?

    What's the error? And, dialog box?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Try Catch not Working?

    Quote Originally Posted by dunfiddlin View Post
    What's the error? And, dialog box?
    its ok i sorted it.

    debug menu>exceptions>...

    I had set all system errors to "THROWN"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width