converting ON ERROR to structured handling
I'm converting an old VB6 project to VB.NET in order to learn how .NET
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:
[old statement 1]
[old statement 2]
and so on....
Is there a better way that I'm missing??
Re: converting ON ERROR to structured handling
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.
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
Catch e As Exception
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
"Thomas" <email@example.com> wrote in message
> I'm converting an old VB6 project to VB.NET in order to learn how .NET
> 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:
> [old statement 1]
> End Try
> [old statement 2]
> End Try
> and so on....
> Is there a better way that I'm missing??
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL