newbie exception handling question

D

Daniel Freedman

Hi,

I'm trying to learn vb.net and have the following scenario I want to
handle:

I'm importing data from a csv file into a dataset. Some of the data is
inconsistent and throws an invalidargument error. What I want to do
with this data is write it to another text file instead of importing
it to the dataset. At the end of the import routine the dataset will
contain all the valid data, and the invalid data will be in another
text file for the user to correct and reprocess.

I can cope with all the reading/writing/catching stuff, but I want to
know if it's possible to continue with the main import code in the try
block once the error has been caught and handled. And if so, how?

So far all I can tell from try...catch...finally blocks is that the
catch block can trap an error and tell the user about it. I can't see
how to get it to fix (or ignore) the error and continue with the main
block of code.

Thanks in advance

Daniel
 
D

Daniel Freedman

Hi,

I'm trying to learn vb.net and have the following scenario I want to
handle:
Don't worry - sussed it out. This is what I'm using now:

While myReader.Peek <> -1
Try
intRows += 1
mystring = intRows.ToString & "," &
myReader.ReadLine & ",,true"

DsCustomers1.Tables("Customers").Rows.Add(mystring.Split(","c))
Catch ex As ArgumentException
MsgBox("argument exception in " & mystring,
MsgBoxStyle.OKOnly)
End Try
End While


whereas before I had the While loop inside the Try block, so when the
exception was thrown the while loop finished.

Daniel
 
C

Cor Ligthert

Daniel,

Your complete problem is in my opinion not difficult to do, have a look
below what I wrote in a kind as pseudo maybe that will do it for you.
Beneath that I write as well something about the try block.

Create your dataset something as

dim ds as new dataset
dim dt as new datatable
ds.tables.add(dt)
dt.columns.add("myfirstcolumn")
dt.columns.add("mysecondcolumn")
etc.

Read your CSV using a streamreader and than while reading line by line

dim dr = dt.newrow
Dim myArray() as string = line.split(whatever splitcriteria)
for i as integer = 0 to myArray.length - 1
dr(i) = myArray(i)
next
dt.rows.add(dr)

Because you said it is inconsistenty you need probably some checking in this

About that try and catch you are not completly clear for me because I do not
see what you try to catch. In this routine above there can normally be no
error when the file exist.

To give more information about your try question, mostly you can fix the
question by placing the try block again in a try block.

I hope this helps?

Cor
 
D

Daniel Freedman

About that try and catch you are not completly clear for me because I do not
see what you try to catch. In this routine above there can normally be no
error when the file exist.

To give more information about your try question, mostly you can fix the
question by placing the try block again in a try block.

I hope this helps?

Hi,

The data inconsistency I'm trying to trap is something like:

a date that has been entered as a text string and will not convert to
a date (i.e. 231483 converts to 23/14/83, which is not valid)

a comma in the middle of an address which will throw out the rest of
the fields.

Thanks for replying, but the code I posted in my own reply (isn't it
always the case when you find the answer just after you've asked the
question?) does what I want it to do.

Cheers

Daniel
 

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

Exception handling again 2
Unhandle exception 5
Exception confusion 4
Datasets and Adapter Updates 3
Retry on exception 3
Exception Handling 8
automatic reboot exception handling 4
Unhandled exception 3

Top