Replacement for Resume Next

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Everyone,
I'm using VS 2005 now and would like to know if .Net has a replacement for
the Resume Next clause. I would like to do something like the following:
On Error Resume Next
value = My.Computer.FileSystem.GetDirectories(Path & "\" & tbl.Name )
if value.Count > 0 then
My.Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
end if
I wanted to see if a Directory existed. But if the main dir is not there,
GetDirectories throws an exception. In this case is good, indicating that its
not there. But I need to the code to continue without going to the Catch
block. Is there a way to do this. Thanks for any info.
Michael
 
Try
value = My.Computer.FileSystem.GetDirectories(Path & "\" & tbl.Name)
Catch ex as New Exception
Finally
If ex.Message.ToString.Length > 0 Then
My.Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
End If
End Try
 
Michael said:
Hi Everyone,
I'm using VS 2005 now and would like to know if .Net has a replacement for
the Resume Next clause. I would like to do something like the following:
On Error Resume Next
value = My.Computer.FileSystem.GetDirectories(Path & "\" &
tbl.Name )
if value.Count > 0 then
My.Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
end if
I wanted to see if a Directory existed. But if the main dir is not there,
GetDirectories throws an exception. In this case is good, indicating that
its
not there. But I need to the code to continue without going to the Catch
block. Is there a way to do this. Thanks for any info.
Michael

Hmm, instead of using exception handling for this, it would probably be
wiser to check for the directory manually and then call GetDirectories. In
VB.Net 2003, we would use:

Imports System.IO
....
Dim dir As String = IO.Path.Combine(Path, tbl.Name)
Dim dirs As String()
If Not Directory.Exists(dir)
IO.Directory.Create(dir)
End If
dirs = IO.Directory.GetDirectories(dir) ' or something to that effect...

HTH,
Mythran
 
Your code is not very robust because any error would cause you to try and
create the directory. Structured exception handling is much easier to use
because you can catch the type of error you really care about. For example:

Try
My.Computer.FileSystem.GetDirectories(Path & "\" & tbl.Name )
Catch Ex as DirectoryNotFoundException
Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
End Try

In this case all other errors will still get raised.
 
Michael said:
Hi Everyone,
I'm using VS 2005 now and would like to know if .Net has a replacement for
the Resume Next clause. I would like to do something like the following:
On Error Resume Next
value = My.Computer.FileSystem.GetDirectories(Path & "\" & tbl.Name )
if value.Count > 0 then
My.Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
end if
I wanted to see if a Directory existed. But if the main dir is not there,
GetDirectories throws an exception. In this case is good, indicating that its
not there. But I need to the code to continue without going to the Catch
block. Is there a way to do this. Thanks for any info.

You could wrap just this line in a Try Catch block. However, it's a
good idea to avoid using exceptions as error handlers when there are
alternatives. In particular here, do you have a reason to avoid the use
of My.Computer.FileSystem.DirectoryExists?

Dim sTableDirectory As String = Path & "\" & tbl.Name
If Not My.Computer.FileSystem.DirectoryExists(sTableDirectory) Then
My.Computer.FileSystem.CreateDirectory(sTableDirectory)
End If
 
You can nest try catch blocks.

try
' This is the outer try block
' Do something
dim value as new collection
try
value = my.computer.filesystem.getdirectories(Path & "\" & tbl.Name)
catch e as exception
value = new collection
finally
if value.count > 0 then my.computer.filesystem.CreateDirectory(Path &
"\" & tbl.Name)
end try
' Continue processing in outer try block
catch outerError as exception
end try


An alternate would be to use

my.computer.filesystem.createdirectory(Path)
value = my.computer.filesystem.getdirectories(path & "\" & tbl.Name)
if value.count > 0 then my.computer.filesystem.CreateDirectory(path & "\"
tbl.Name)

or even shorter if you don't care about "value" when you're done (the My
Classes hide a lot of exception handling)

my.computer.filesystem.CreateDirectory(path & "\" & tbl.Name

Mike.
 
Michael said:
I'm using VS 2005 now and would like to know if .Net has a replacement for
the Resume Next clause. I would like to do something like the following:
On Error Resume Next
value = My.Computer.FileSystem.GetDirectories(Path & "\" &
tbl.Name )
if value.Count > 0 then
My.Computer.FileSystem.CreateDirectory(Path & "\" & tbl.Name)
end if

\\\
Imports System.IO
..
..
..
Dim Path As String = "C:\Foo"
If Not Directory.Exists(Path) Then
Directory.CreateDirectory(Path)
End If
///

This even works in .NET 1.0/1.1 :-).
 
Hi Everyone,
Thanks for all the replys.

Larry, I did a little reading on the new MY commands, but didn't know that
was in there also. I think I like this better than trying to use the
Try/Catch blocks. I will look into this deeper. Thanks again everyone for the
suggestions.
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Resuming Try 4
On Error Resume Next 25
Resuming from an error 5
resume next in VB.NET ? 10
On error resume next bug 4
errorhandling with resume next... 2
Try catch and Resume 15
How to implement "On Error Resume"? 6

Back
Top