PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2013 [RESOLVED] Exception handing block not handling an exception-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] Exception handing block not handling an exception

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Resolved [RESOLVED] Exception handing block not handling an exception

    vb.net Code:
    1. Try
    2.     Using objResponse As HttpWebResponse = DirectCast(objRequest.GetResponse(), HttpWebResponse)
    3.         Using objReader As New StreamReader(objResponse.GetResponseStream())
    4.             strContents = objReader.ReadToEnd
    5.         End Using
    6.     End Using
    7. Catch ex As WebException
    8.     blnSuccess = False
    9. End Try
    This code exists in an object which downloads the contents of a web page. It exists in another object which is being used in a background worker object. Rarely I get an unhandled exception 'System.IO.IOException' caused by the objPostReader.ReadToEnd method with the error "Received an unexpected EOF or 0 bytes from the transport stream.". Apparently once in a blue moon it coughs up a hairball. But I thought I had handled that by having it in the Try/Catch/End block. When I wrote this code I had a discussion here about what parts could cause exceptions and we decided it was best to have both the ReadToEnd and the GetResponseStream in the same exception handling block. So why is the background worker object terminating with an unhandled exception? Is there something about the 'Using' block that doesn't allow the handler to handle the exception?

  2. #2
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: Exception handing block not handling an exception

    Are you sure you are catching the right exception? It may not be cause by a WebException but an IOException.
    Try validate that properly.
    Nothing I post is Self Reliable. Use it at your own risk

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,352

    Re: Exception handing block not handling an exception

    The exception isn't being handled because you are not handling that kind of exception... it is a System.IO.IOException, but you only handle WebException.

    If you want to handle that type of exception, you can add an appropriate Catch block for it.

    If you want to handle all exceptions, and deal with them in the same manner, use a 'generic' Catch block (ie: only specify Exception, rather than any specific ones).

  4. #4

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Re: Exception handing block not handling an exception

    I am sure. I stopped the background worker object when the error occurred and could see the stack trace and everything of the exception.

    I'm reading now that the Using block doesn't allow the Try to access the bits within. I'm trying to find a good explanation so I can come up with a plan to change the exception handler. I think I might need two in this case.

  5. #5

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Re: Exception handing block not handling an exception

    Oh! I see. So if I replace "Catch ex As WebException" with "Catch ex As Exception" it will handle the exception?

  6. #6
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: Exception handing block not handling an exception

    Okay try taking off those using blocks and declare them like normal variables, then dispose whatever object you'll be using by adding a finally block and placing the code inside.
    Nothing I post is Self Reliable. Use it at your own risk

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,352

    Re: Exception handing block not handling an exception

    Quote Originally Posted by cory_jackson View Post
    Oh! I see. So if I replace "Catch ex As WebException" with "Catch ex As Exception" it will handle the exception?
    Yes, assuming of course that the exception is happening in the Try block, rather than in another part of the routine.

  8. #8

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Re: Exception handing block not handling an exception

    Quote Originally Posted by si_the_geek View Post
    Yes, assuming of course that the exception is happening in the Try block, rather than in another part of the routine.
    I have read other posts that make it sound like if can't see the variables within the Using block. But maybe that's when they're declared in the Using block. But this is what I need. I was thinking I can create an exception to test. I'll report on what I find. Thanks for spotting the exception versus web execption. Good eyes!

  9. #9

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Re: Exception handing block not handling an exception

    That was it. I created a divide by zero exception and it behaved like I first described with the "Received an unexpected EOF or 0 bytes from the transport stream." causing the background worker to abort. Then I change the exception handler from WebException to Exception and my normal exception handling worked as I intended. Thank you for helping identify my mistake.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,756

    Re: Exception handing block not handling an exception

    You can also have multiple Catch blocks with any Try block, so you can Catch Ex as WebException followed by Catch ex as IOException. That way you can handle different types of exceptions differently without adding any code to figure out the type of exception within the Catch block itself.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    698

    Re: Exception handing block not handling an exception

    Quote Originally Posted by Shaggy Hiker View Post
    You can also have multiple Catch blocks...
    I had no idea. That's awesome. I don't often care about or do anything with the exception, but it's good to know this is an option. Thank you.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width