Try Catch does not Execute Catch code
-
Hi all. I was trying this TreeView demo and ran into a situation where a Try Catch block will not pass execution onto the Catch code. The error is "Drive not ready" when attempting to access a:\. That is easy enough to understand...I have no floppy on my system. But why does the error handler not pass the error to the Catch area? Many thanks for any advice! Here is the demo: A form just has a TreeView and a button on it. The demo populates a TreeView with the directories of my file system:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get a list of drives
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.Empty'Now loop thru each drive and populate the treeview: For i As Integer = 0 To drives.Count - 1 rootDir = drives(i).Name 'Add this drive as a root node: TreeView1.Nodes.Add(rootDir) 'Populate this root node: PopulateTreeView(rootDir, TreeView1.Nodes(i)) Next
End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.EmptyTry Dim folders() As String = IO.Directory.GetDirectories(dir) If folders.Length <> 0 Then Dim childNode As TreeNode = Nothing For Each folder In folders childNode = New TreeNode(folder) parentNode.Nodes.Add(childNode) PopulateTreeView(folder, childNode) Next End If Catch ex As UnauthorizedAccessException parentNode.Nodes.Add(folder & ": Access Denied") End Try
End Sub
End Class
-
Hi all. I was trying this TreeView demo and ran into a situation where a Try Catch block will not pass execution onto the Catch code. The error is "Drive not ready" when attempting to access a:\. That is easy enough to understand...I have no floppy on my system. But why does the error handler not pass the error to the Catch area? Many thanks for any advice! Here is the demo: A form just has a TreeView and a button on it. The demo populates a TreeView with the directories of my file system:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get a list of drives
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.Empty'Now loop thru each drive and populate the treeview: For i As Integer = 0 To drives.Count - 1 rootDir = drives(i).Name 'Add this drive as a root node: TreeView1.Nodes.Add(rootDir) 'Populate this root node: PopulateTreeView(rootDir, TreeView1.Nodes(i)) Next
End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.EmptyTry Dim folders() As String = IO.Directory.GetDirectories(dir) If folders.Length <> 0 Then Dim childNode As TreeNode = Nothing For Each folder In folders childNode = New TreeNode(folder) parentNode.Nodes.Add(childNode) PopulateTreeView(folder, childNode) Next End If Catch ex As UnauthorizedAccessException parentNode.Nodes.Add(folder & ": Access Denied") End Try
End Sub
End Class
-
You are only catching
UnauthorizedAccessException
, which is not the same as Drive not ready.Use the best guess