Try / Catch and Resume Execution

J

Joe Rigley

Hello,

As a .NET newbie I'd appreciate some advice... I've been tasked with
writing a small app (in VB .NET) that takes some data from our HR system,
does some format modifications, and loads that into Active Directory.

For error handling of this app, I was considering having the app process
each employee one at a time. If an error occurs, I was thinking that the
system would handle the error with a Try/Catch, send the error details to me
in an email and then attempt to continue processing the next employee. If
more than 3 errors occur within a single run of the job, it would send
another email stating a max number of errors has occurred and then terminate
the app via code.

Based on that info I have 2 questions: 1. Is this a good design for an app
like this? 2. If the design is reasonable, how do I get the Try / Catch
code to resume processing?

All advice is welcomed.

Thanks,
-Joe
 
C

Cor Ligthert [MVP]

Joe,

A try and catch is to get unpredictable errors the first error that happens
should be more than enough for your action.

By instance a lan connection cable is unplugged.

Or in a database, while you where updating information, somebody else
changed the base of that. Think on a bankingaccount where the saldo was 100
you added 10 so the new value that you will write is 110. However somebody
else changed the saldo to 90 before you.

Therefore it is better to test using by instance an if what is the error and
than take the next step in by instance a loop.

A try and catch does this language independent

Try
try to do what is told
Catch
catch an error if that is imposible
Finally
do someting despitie if there was a catch even if in the catch was a
return
End

I hope this gives an idea

Cor
 
J

Joe Rigley

Cor,

Thanks for the reply. Unfortunately, I'm not following you... Are you
saying the design of this app should be re-thought? If yes, would you offer
another example? I think I understand what you mean in regards to the
possibility of having data being updated by two different sources, thus
being out of sync, however I do not follow how that applies here. Can you
explain further? (There is no possibility of having another data source
updating this data when this job is running.)

From what I have read, a Try / Catch can be utilized to trap both
predictable and unpredictable types of errors. For example, if I have a
Int16 variable and I assign a very large integer value to that variable, an
Overflow exception will occur. I can write a specific Catch to handle that
specific type of error:
Catch ex as System.OverflowException

Or, I can use the generic Catch to handle all other errors:
Catch ex as System.Exception

Either way I am attempting to handle the error. My reasoning for allowing
the job to continue processing is purely for data cleansing. I'm not 100%
confident all of the data will exist or be in the proper format. Thus, the
approach of complete the rest of job and just send me the errors to review.
Does that make more sense?

Please offer your thoughts.

Thanks,
-Joe
 
G

gmiley

Basically, what you want to do is a vary viable solution.

Using try/catch to handle a possible error elegantly is what it is
there for.

you could have something similar:

try
// code
catch
// incriment the error count (private int of some kind)
// add a conditional statement here to check a counter.
// if the counter is 0 send an email
// if the counter is MAX_ERR (your constant defining the obvious)
// then execute code to clean up, cancel the looping, and send an
email.
 
M

Marina

Presumably you have some sort of loop that is doing something for each
employee?

I would imagine the algorithm would be something like:

numberOfFailures = 0
While (there are more employees to process)
Try
ProcessNextEmployee
Catch
SendErrorEmail
numberOfFailures += 1
End Try

IF numberOfFailures >= 3
SendErrorEmail
Exit While
End If
End While
 
C

Cor Ligthert [MVP]

Joe,

Using the sample of Marina than would it in my opinion be nicer to do.

Joe this is possible however hot the purpose of try and catch are you are
able to do.

\\\
dim numberOfFailures as integer
While (there are more employees to process)
If (there is after testing something wrong) then
SendErrorEmail
numberOfFailures += 1
if numberOfFailures > 2 then
SendErrorEmailPlus
exit While
end if
End if
///

Then you have clean code while you have not used the Exceptions which is at
least for the first consuming time and needs more processing than normally
this.

The try and catch is for what you can not get by normal testing.

Cor
 

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

Top