converting ON ERROR to structured handling


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: converting ON ERROR to structured handling

  1. #1
    Thomas Guest

    converting ON ERROR to structured handling

    Hi.

    I'm converting an old VB6 project to VB.NET in order to learn how .NET
    works.

    I understand Try/Catch blocks, but I'm trying to convert a section of code
    that used ON ERROR RESUME NEXT in VB. If an error occurs in most of this
    block, I just ignore it. This approach is fine for the routine at hand
    because errors are generally caused by missing data - in this case, the
    defaults are fine.

    What is the best way to convert this type of code to structured error
    handling? From reading the docs, the best I can come up with is:

    Try
    [old statement 1]
    Finally
    End Try
    Try
    [old statement 2]
    Finally
    End Try

    and so on....

    Is there a better way that I'm missing??




  2. #2
    Jason Sobell \(iGadget\) Guest

    Re: converting ON ERROR to structured handling

    Hi Thomas,
    There are very few situations that call for the ON ERROR RESUME NEXT
    construct in VB, since every line in VB6 would also require an "IF ERR<>0
    THEN" statement after it to correctly trap the situation.
    In VB.NET most of the situations that needed error trapping (such as missing
    keys from collections due to lack of a .Contains method) have been resolved,
    and most remaining ones can be handled by wrapping the command in it's own
    function to handle the error.
    e.g.
    Instead of

    On Error Resume Next
    a = myCollection("A")
    b = myCollection("B")
    c = myCollection("C")
    d = myCollection("D")
    On Error Goto 0

    you can now use either a check such as
    If myCollection.Contains("A") Then a=myCollection("A")
    If myCollection.Contains("B") Then a=myCollection("B")
    If myCollection.Contains("C") Then a=myCollection("C")
    If myCollection.Contains("D") Then a=myCollection("D")

    or (if there is a definite possibility of errors occuring)

    a = LookupValue(a, myCollection, "A",a)
    b = LookupValue(b, myCollection, "B",b)
    c = LookupValue(c, myCollection, "C",c)
    d = LookupValue(d, myCollection, "D",d)

    Private Function LookupValue(mc as Collection, key a String, default as
    Long) As Long
    Try
    return myCollection(key)
    Catch e As Exception
    return default
    End Try
    End Function

    This last method is much more structured in that it allows you to add
    logging, any kind of checking of data contents, and (most importantly) a
    single point in the code to examins _why_ the error occurred. You might not
    care about a 'Key not found' error for missing entries, but you would
    probably want to do something more significant with a 'permission denied' or
    'out of memory' error.

    One of the tricky things in the move from VB6 to VB.NET is that some of the
    old commonly used constructs were brought over from some nice but cludgy
    ideas in the early versions of VB, and although things like Try/Catch seem
    alien (and a bit tedious) in VB.NET, they are actually an enormous
    improvement once you get into the habit of using them regularly

    Cheers,
    Jason

    "Thomas" <thomas3617@spamcop.net> wrote in message
    news:3dbeec52$1@tnews.web.devx.com...
    > Hi.
    >
    > I'm converting an old VB6 project to VB.NET in order to learn how .NET
    > works.
    >
    > I understand Try/Catch blocks, but I'm trying to convert a section of code
    > that used ON ERROR RESUME NEXT in VB. If an error occurs in most of this
    > block, I just ignore it. This approach is fine for the routine at hand
    > because errors are generally caused by missing data - in this case, the
    > defaults are fine.
    >
    > What is the best way to convert this type of code to structured error
    > handling? From reading the docs, the best I can come up with is:
    >
    > Try
    > [old statement 1]
    > Finally
    > End Try
    > Try
    > [old statement 2]
    > Finally
    > End Try
    >
    > and so on....
    >
    > Is there a better way that I'm missing??
    >
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center