Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MAJOR Try...Catch...Finally BUG!

5 views
Skip to first unread message

David Williams

unread,
May 10, 2002, 9:32:36 PM5/10/02
to
I have found what seems to be a MAJOR bug with VB.NET's structure error handling when working with XML types.  I have confirmed that I can cause this behavior on different systems.  Has anyone else run across this?
 
Try the following code in a new Console Application:
Option Strict On
Imports System
Imports System.Xml
Module Module1
Sub Main()
Dim testXML As XmlDocument = New XmlDocument()
Dim steps As XmlNodeList
Dim tag As XmlNode
Dim retVal As XmlNode
Console.WriteLine("Starting Main")
Try
testXML.LoadXml("<root><item><test cond='false'/></item></root>")
steps = testXML.SelectNodes("*/item")
For Each tag In steps
    retVal = Test(tag, Nothing)
Next
Catch ex As Exception
    Console.WriteLine("Main caught: " & ex.Message)
Finally
    Console.WriteLine("Press enter to exit")
    Console.ReadLine()
End Try
    End Sub
Private Function Test(ByVal tag As XmlNode, ByVal dummy As XmlNode) As XmlNode
Dim selectedTag As XmlNode = Nothing
Dim selected As XmlElement
Dim att As String
Console.WriteLine("In Test")
Try
If tag Is Nothing Then
    Exit Try
End If
For Each selectedTag In tag

selected = CType(selectedTag, XmlElement)
If dummy Is Nothing Then
    att = selected.GetAttribute("cond")
    If att = "false" Then
        Console.WriteLine("Found attribute cond = false")
        Exit Try
    End If
Else
    If selectedTag.Name = dummy.Name Then
        Exit Try
    End If
End If
Next
selectedTag = Nothing
Console.WriteLine("SelectedTag is nothing")
Catch ex As Exception
    Console.WriteLine("Test caught: " & ex.Message)
Finally
    Console.WriteLine("Test Done")
End Try
Return selectedTag
End Function
End Module

Mattias Sjögren

unread,
May 10, 2002, 10:28:27 PM5/10/02
to
David,

>I have found what seems to be a MAJOR bug with VB.NET's structure error handling when working with XML types. I have confirmed that I can cause this behavior on different systems. Has anyone else run across this?

What, exactly, is this MAJOR bug you're referring to? I tried your
code, and got the output

Starting Main
In Test


Found attribute cond = false

SelectedTag is nothing
Test Done
Press enter to exit


Looks correct to me.


Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/

Armin Zingler

unread,
May 11, 2002, 6:13:40 AM5/11/02
to
"Mattias Sjögren" <mattias.don...@mvps.org> schrieb

> David,
>
> >I have found what seems to be a MAJOR bug with VB.NET's
> > structure error handling when working with XML types. I have
> > confirmed that I can cause this behavior on different systems.
> > Has anyone else run across this?
>
> What, exactly, is this MAJOR bug you're referring to? I tried your
> code, and got the output
>
> Starting Main
> In Test
> Found attribute cond = false
> SelectedTag is nothing
> Test Done
> Press enter to exit
>
>
> Looks correct to me.

I can agree.

Armin

David Williams

unread,
May 11, 2002, 7:41:06 AM5/11/02
to
Ok, let me be more clear. I have reduced the code and explained a little:

I have found what seems to be a MAJOR bug with VB.NET's structured error


handling when working with
XML types.

An "Exit Try" statement is designed to do exactly that, exit the Try block
via the Finally area without
running any addtional code from within the Try block. In this case, the
"Exit Try" is performing an
"Exit For" instead of an "Exit Try". I tried the same structures without
using the XML types and it
works as expected... the next statement executed after the "Exit Try" is the
first statement of the
"Finally" block.

I have confirmed that I can cause this behavior on different systems. Has
anyone else run across this?

If the code that follows worked as expected, I should see this output:

Starting Main
In the If Block


Test Done
Press enter to exit

however what I see is this:

Starting Main
In the If Block
Exited Inner For Loop


Test Done
Press enter to exit

Here is the test code that I used to duplicate the problem. In a new
Console Application:

Option Strict On
Imports System
Imports System.Xml

Module Module1

Sub Main()

Sub Main()
Dim testXML As XmlDocument = New XmlDocument()
Dim steps As XmlNodeList
Dim tag As XmlNode
Dim retVal As XmlNode

Dim selectedTag As XmlNode = Nothing

Console.WriteLine("Starting Main")


Try
testXML.LoadXml("<root><item><test cond='false'/></item></root>")
steps = testXML.SelectNodes("*/item")
For Each tag In steps

Try


For Each selectedTag In tag

If 1 = 1 Then
Console.WriteLine("In the If Block")
Exit Try
Console.WriteLine("After the Exit Try")
End If
Console.WriteLine("After If Block")
Next

Console.WriteLine("Exited Inner For Loop")

Catch ex As Exception
Console.WriteLine("Test caught: " & ex.Message)

Finally
Console.WriteLine("Test Done")

End Try

Exit For


Next
Catch ex As Exception
Console.WriteLine("Main caught: " & ex.Message)
Finally
Console.WriteLine("Press enter to exit")
Console.ReadLine()
End Try

End Sub


End Module

"Armin Zingler" <armin.nixs...@gmx.de> wrote in message
news:uvpDyrN#BHA.2312@tkmsftngp04...

Mattias Sjögren

unread,
May 11, 2002, 9:16:13 AM5/11/02
to
David,

Alright, now I see what you're saying. However, this has nothing to do
with the Xml classes (the compiler doesn't handle them specially), it
can happen for any non-array collection. I reduced the code a bit to
make it easier to see what happens

Sub Main()
Dim i As Integer
Dim al As New ArrayList

al.Add( 1 )
al.Add( 2 )
al.Add( 3 )

Try
Console.WriteLine("Before For Each loop")

For Each i In al
Console.WriteLine( i )
If i = 2 Then Exit Try
Next

Console.WriteLine("After For Each loop - should never get here")

Finally
Console.WriteLine("In Finally")
End Try

End Sub


The problem is that a For Each block creates an implicit
Try..Catch..Finally block (so there are actually two of those in the
code above) and the code generated for Exit Try exits the wrong one.
This is definitely a bug, good catch!

David Williams

unread,
May 11, 2002, 9:34:55 AM5/11/02
to
Ok, I agree why it does not have to be the XML types. I was working with
them when I found the problem, and was not able to reproduce the problem
using integers. I did not abstract it far enough.

"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:#fJLT3O#BHA.1868@tkmsftngp04...

0 new messages