Home All Groups Group Topic Archive Search About

Working with exceptions...

Author
15 Aug 2006 9:04 PM
Military Smurf
Okay, so this borrowed code is for doing a recursive file search.  The
problem is when I encounter the System Volume Information folder, a special
kind of exception is thrown that ends up terminating the subroutine.  I have
two other exceptions that are thrown before the one I described does, yet
they don't terminate the subroutine.

Can anyone tell me why one kind of exception terminates this subroutine,
while another does not?



Sub FileSearch(ByVal sDir As String, ByVal FileName As String)
        Dim Trigger As Integer = 0
        Dim d2 As String
        Dim f2 As String
        If Trigger = 0 Then
            Try
                For Each d2 In Directory.GetDirectories(sDir)
                    For Each f2 In Directory.GetFiles(d2, FileName)
                        MsgBox(f2, MsgBoxStyle.Critical, "File Found")
                    Next
                    FileSearch(d2, FileName)
                Next
            Catch excpt As UnauthorizedAccessException
                MsgBox(excpt.Message)
            End Try
        End If
     'MsgBox("Done")
    End Sub

Author
15 Aug 2006 11:45 PM
Tom Shelton
Military Smurf wrote:
> Okay, so this borrowed code is for doing a recursive file search.  The
> problem is when I encounter the System Volume Information folder, a special
> kind of exception is thrown that ends up terminating the subroutine.  I have
> two other exceptions that are thrown before the one I described does, yet
> they don't terminate the subroutine.
>
> Can anyone tell me why one kind of exception terminates this subroutine,
> while another does not?
>
>

Sure, the code below is only setup to capture
UnauthorizedAccessException.  Any other exception is going to be thrown
right out of the routine...  What's the fix?  Well, you can either add
another catch statement for the specific exception or you can modify
the code to trap all exceptions by changeing the catch block to read:

Catch excpt As Exception

I caution yout that this is in general not good practice, as it can
cause important exceptions to be ignored...  But, it is a possible
solution.

Show quoteHide quote
>
> Sub FileSearch(ByVal sDir As String, ByVal FileName As String)
>         Dim Trigger As Integer = 0
>         Dim d2 As String
>         Dim f2 As String
>         If Trigger = 0 Then
>             Try
>                 For Each d2 In Directory.GetDirectories(sDir)
>                     For Each f2 In Directory.GetFiles(d2, FileName)
>                         MsgBox(f2, MsgBoxStyle.Critical, "File Found")
>                     Next
>                     FileSearch(d2, FileName)
>                 Next
>             Catch excpt As UnauthorizedAccessException
>                 MsgBox(excpt.Message)
>             End Try
>         End If
>      'MsgBox("Done")
>     End Sub

--
Tom Shelton [MVP]