469,579 Members | 1,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Try / Catch and Resume Execution

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
Aug 26 '05 #1
9 2117
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
Aug 26 '05 #2
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

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
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

Aug 26 '05 #3
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.

Aug 26 '05 #4
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

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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

Aug 26 '05 #5
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
Aug 26 '05 #6
Thank you for the reply. (To everyone for contributing...)

Let me clarify one last thing and I should be all set. Using the code below
as an example is this scenario true? The application is executing within
the Try and an Exception occurs. The apps execution path would now move to
the Catch section. The code in the Catch block is executed. After the
application executes the End Try statement, will the program continue to run
and perform the "IF numberOfFailures >= 3" statement and all other
statements that may exist below that? Or would the program terminate after
it reaches the End Try statement?

Once again, thanks very much to all that have contributed to this thread.

-Joe
"Marina" <so*****@nospam.com> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
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

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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


Aug 26 '05 #7
Yes, of course. The idea behind the Try/Catch is to catch exception that
may occur in a particular block of code. If the exception is caught, then
after the Catch is execute, the next line of code is executed.

Otherwise, any time you had any exception of any sort, your application
would have to completely shut down.

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Thank you for the reply. (To everyone for contributing...)

Let me clarify one last thing and I should be all set. Using the code
below as an example is this scenario true? The application is executing
within the Try and an Exception occurs. The apps execution path would now
move to the Catch section. The code in the Catch block is executed.
After the application executes the End Try statement, will the program
continue to run and perform the "IF numberOfFailures >= 3" statement and
all other statements that may exist below that? Or would the program
terminate after it reaches the End Try statement?

Once again, thanks very much to all that have contributed to this thread.

-Joe
"Marina" <so*****@nospam.com> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
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

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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



Aug 26 '05 #8
As Marina stated, try/catch is simply to handle exceptions at the code
level before it causes your application to choke.

the catch block is where you put whatever code you want to execute
should an exception of the type you are catching happens. Most people
(in most circumstances) will usually stop the entire process if there
is an exception and return to the calling method gracefully with a
"return" or rethrowing the exception to allow it to gracefully bubble
up to a master exception handler.

For other circumstances you may just want to trap the exception, in
your case notify someone via email, and continue normal opperation
until a set number of exceptions occurrs or the process completes.

const int MAX_ERRCNT = 3;

int exCount = 0;
foreach(type item in itemcollection)
{
try
{
item.Process();
}
catch (System.Exception ex)
{
exCount++;
Emailer.SendErrorNotice(ex);
if(exCount >= MAX_ERRCNT)
{
throw;
}
}
}

Aug 26 '05 #9
Thanks for the fantastic information. I really appreciate everyone's time.
Now, I have a much better understanding of Try / Catch functionality and
usage.

Have a great weekend all!
-Joe

"Marina" <so*****@nospam.com> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
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

"Joe Rigley" <jc******@spartanmotors.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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


Aug 26 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rob R. Ainscough | last post: by
9 posts views Thread by Joe Rigley | last post: by
4 posts views Thread by DavideR | last post: by
15 posts views Thread by Neo | last post: by
2 posts views Thread by AB | last post: by
7 posts views Thread by Rob R. Ainscough | last post: by
3 posts views Thread by E. Kwong | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.