473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Replacement for Resume Next

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

Dec 15 '05 #1
7 2536
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" <Mi*****@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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

Dec 15 '05 #2

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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

Dec 15 '05 #3
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" wrote:
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

Dec 15 '05 #4

Michael wrote:
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(sTableDirec tory) Then
My.Computer.FileSystem.CreateDirectory(sTableDirec tory)
End If

--
Larry Lard
Replies to group please

Dec 15 '05 #5
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.

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
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


Dec 15 '05 #6
"Michael" <Mi*****@discussions.microsoft.com> schrieb:
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 :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 15 '05 #7
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
"Larry Lard" wrote:

Michael wrote:
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(sTableDirec tory) Then
My.Computer.FileSystem.CreateDirectory(sTableDirec tory)
End If

--
Larry Lard
Replies to group please

Dec 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
2
by: Darta | last post by:
OK- I volunteer to be shot if this question is stupid... BUT, is there a way to, when you catch an exception in the catch{} handler, resume to the same line of code that generated an error while...
3
by: Dave | last post by:
Hi, I have a C# program that is parsing an XML file and loading a database table. Some of the elements may be missing but I want to continue with loading the data anyway because they may not...
5
by: itsupport1 | last post by:
Hi, I am importing some records from one table to another table, and due to Constraints in the destination table, it Throws an Exception and Skip the Whole Rest of records. So I did implement...
3
by: bob.needler | last post by:
I know On Error Resume Next is generally considered lazy. But can someone tell me why the resume next in Exit_Handler does not seem to work? It generates the typical unhandled runtime error...
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
11
by: Maxwell2006 | last post by:
Hi, I know that this is not a good practice, but I wonder do we have "on error resume next" in C#? Thanks,
11
by: fniles | last post by:
In VB 6 I can do the following: Sub MySub on error goto Err1 : --all my codes are here : --say this is where the error occurs : --this is where resume next will bring me after Err1 exit sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.