ERL in VB.Net by ThinqLinq

ERL in VB.Net

A while back I posted that the ERL function is still available in Vb.Net. I was prompted to re-look at the code from a recent newsgroup post and realized that, while it is possible to use ERL, there is a definite performance penalty. Consider the original sample as below:

10: REM This program doesn't do much
20: Try
30: REM Throw an error
40: Throw New DivideByZeroException
50: Catch ex As DivideByZeroException
60: Console.WriteLine("Error occured in line: " & Erl())
70: End Try
80: Console.ReadLine()

Using Lutz Roeder's excellent reflector, we can see that the VB compiler does some changes behind the scene. Here is the code as decompiled from the IL:

Dim num1 As Integer = 10
num1 = 20
num1 = 30
num1 = 40
Throw New DivideByZeroException
Catch exception2 As DivideByZeroException
ProjectData.SetProjectError(exception2, num1)
Dim exception1 As DivideByZeroException = exception2
num1 = 60
Console.WriteLine(("Error occured in line: " & Conversions.ToString(Information.Erl)))
num1 = 70
End Try
num1 = 80

It becomes obvious that the VB compiler is adding a tracking variable (num1) and updating it similar to a trace.write method. I highlighted the original code segments using an old Commodore 64 color scheme. There is additional overhead as well to push the line number into a storage mechanism for ERL to be able to grab it as displayed by the highlighted code segments.

Conclusion: while it is possible to use ERL, there are often better ways of achieving the same result, most notably a well designed tracing mechanism with trace level switches.

Posted on - Comment
Categories: VB) -
comments powered by Disqus