How do I properly Try...Catch...Finally "InsufficientMemoryException" in Visual Basic RRS feed

  • Question

  • This is much like what I did...

    Sub mysub()
    Dim test As Runtime.MemoryFailPoint = New()Runtime.MemoryFailPoint
    dim x as Integer
    ' do some normal calculations to get the size of some arrays plus a fudge factor of a few hundred bytes to a size in MB and  assign that to the Integer
    x = results
    console.writeline("How big IS it: {0} MB", x)
    test = Runtime.MemoryFailPoint(x)
    Catch e As InsufficientMemoryException
    console.writeline("Yep, it failed: {0}", e.message)
    'some code that set a value outside this sub in my code so
    I'd know not to proceed and deal with this issue first
    End Try
    End Sub

    This worked fine just as long as "x" didn't fail the check. If it did fail the check I would get the most bizarre output in the console.

    How big IS it: 2050 MB
    Yep, it failed: long error message text
    Yep, it failed: long error message text 'yes it's an exact duplicate
    How big IS it: 2050MB

    The program continued and seemed fine, not that I checked too far, but it didn't crash and I did get indication that I likely could not make the desired assignment without problems. (I did try it by brute force just to see if it really would crash, and it did consistently so the report is quite likely valid in any given case).

    Not having seen this kind of behavior from other try...catch blocks for ArgumentException s thrown I'm horribly curious what I'm doing wrong. I could not find a way to get the warning about potentially exceeding memory limits (and thus getting an OutOfMemoryException if I proceeded) without having it appear that my sub was getting called twice (once backwards?) only when it failed the check.

    I realize I'm swimming in deep waters here, but I'm still curious. If there is no simple answer, that's fine... I don't NEED the information anymore. It would just be a nice safeguard if I can get it to work without looking weird for no known reason (and possibly kicking unhandled exceptions up the chain for no purpose).

    Tuesday, August 29, 2017 11:14 PM

All replies

  • Catch e As InsufficientMemoryException console.writeline("Yep, it failed: {0}", e.message)

    Catch ex as Exception console.writeline("It failed on general exception catch all and any')

    Major exception catch and then general exception catch, the failsafe, using back to back exceptions handling.  

    The major one is caught, the general one is ignored. The major one not caught, then the catch all general one is used.

    You are not doing anything with the Finally other than doing an Dispose() use the Using statement to do the Dispose().


    If you need finer control over the acquisition of the resources, or you need additional code in the Finally block, you can rewrite the Using block as a Try...Finally construction. The following example shows skeleton Try and Using constructions that are equivalent in the acquisition and disposal of resource.


    Wednesday, August 30, 2017 2:03 AM