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 XmlNodeConsole.WriteLine("Starting Main")
TrytestXML.LoadXml("<root><item><test cond='false'/></item></root>")
steps = testXML.SelectNodes("*/item")
For Each tag In steps
retVal = Test(tag, Nothing)
NextCatch 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 XmlNodeDim selectedTag As XmlNode = Nothing
Dim selected As XmlElement
Dim att As StringConsole.WriteLine("In Test")TryIf tag Is Nothing Then
Exit Try
End IfFor 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 IfNextselectedTag = Nothing
Console.WriteLine("SelectedTag is nothing")Catch ex As Exception
Console.WriteLine("Test caught: " & ex.Message)Finally
Console.WriteLine("Test Done")End TryReturn selectedTagEnd Function
End Module
>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/
I can agree.
Armin
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...
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!
"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:#fJLT3O#BHA.1868@tkmsftngp04...