Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb_dotnet thread: Catch error using Try - Catch - End Try while in loop


Message #1 by "Seth Bembeneck" <sbembeneck@c...> on Sat, 18 May 2002 06:08:32
Thanks... It works.

> Hi Seth,

There are multiple locations in your code that can cause an error, so 
you'll need multiple try catch statements. The folder.GetFiles method can 
cause an error because you don't have access to the folder, but the 
folder.GetDirectories method can also cause an error when you don't have 
access to it.

Try (or catch ;-) ) this instead:

         Try
             For Each file In folder.GetFiles(DirPath)
                 FI = New FileInfo(file)
                 lstFileList.Items.Add(FI.FullName)
                 lblFileCount.Text = "Total amount of files: " + _
                         lstFileList.Items.Count.ToString
             Next

             Try
                 For Each dir In folder.GetDirectories(DirPath)
                     Application.DoEvents()
                     AddFolder(dir)
                 Next
             Catch e As Exception
                 MessageBox.Show(e.ToString())
             End Try
         Catch ex As Exception
             MessageBox.Show(ex.ToString())
         End Try


You may want to consider defining multiple catch statements, from very 
specific errors (like UnauthorizedAccessException) to more general (like 
Exception) to be able to give a more precise error message back to the 
user.

HtH

Imar



At 11:10 PM 5/18/2002 +0000, you wrote:
>Here is the full sub:
>
>Private Sub AddFolder(ByVal DirPath as string)
>'This sub will add the files in the Directory path passed to it (DirPath)
>'It will also call it's self every time there is a sub directory.
>
>         Dim dir As String
>         Dim file As String
>         Dim FI As FileInfo
>         Dim folder As Directory
>
>         'The line below is what generates the error. The error msg is at
>         'the end of the code.
>         For Each file In folder.GetFiles(DirPath)
>             Try
>                 FI = New FileInfo(file)
>                 lstFileList.Items.Add(FI.FullName)
>                 lblFileCount.Text = "Total amount of files: " + _
>                         lstFileList.Items.Count.ToString
>             Catch exc As Exception
>                 MsgBox(exc.Message)
>             End Try
>
>         Next
>
>         For Each dir In folder.GetDirectories(DirPath)
>             Application.DoEvents()
>             AddFolder(dir)
>
>         Next
>
>     End Sub
>
>Error Message:
>An unhandled exception of type 'System.UnauthorizedAccessException'
>occurred in mscorlib.dll
>
>Additional information: Access to the path C:\Documents and Settings\Seth
>Bembeneck\My Documents\Downloads\Computer Pranks\add-remove is denied.
>
>If I do the try catch like this:
>
>try
>    For Each file In folder.GetFiles(DirPath)
>    FI = New FileInfo(file)
>    lstFileList.Items.Add(FI.FullName)
>    lblFileCount.Text = "Total amount of files: " + _
>         lstFileList.Items.Count.ToString
>    next
>
>catch exc as Exception
>    'Handling code here
>
>end try
>
>It works, but the for next loop will not continue. What I need to be able
>to do is go through every sub-folder in a folder and if a particular
>folder cannot be accessed for any reason, just to skip it and continue 
on.
>
>Hope this helps.
>Seth



  Return to Index