Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Try Catch does not Execute Catch code

Try Catch does not Execute Catch code

Scheduled Pinned Locked Moved Visual Basic
questionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    treddie
    wrote on last edited by
    #1

    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.Empty

    Try
      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

    L 1 Reply Last reply
    0
    • T treddie

      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.Empty

      Try
        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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are only catching UnauthorizedAccessException, which is not the same as Drive not ready.

      Use the best guess

      T 1 Reply Last reply
      0
      • L Lost User

        You are only catching UnauthorizedAccessException, which is not the same as Drive not ready.

        Use the best guess

        T Offline
        T Offline
        treddie
        wrote on last edited by
        #3

        Ahhhhh, OK. Was right in front of me the whole time. Thanks for the brain reset.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups