473,397 Members | 2,028 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,397 software developers and data experts.

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 2321
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
5
by: Rob R. Ainscough | last post by:
Coming from VB6 to VB.NET, it appears if I opt to use the Try Catch approach I don't have any way to RESUME from within my catch statement? What I often do is resolve the problem in my catch...
9
by: Joe Rigley | last post by:
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...
4
by: DavideR | last post by:
I have converted a large vb6 program with an add-in that for every routine gimme the error handling: the add-in adds one line on the head of the routine "if myerrhandle then ON ERROR GOTO...
15
by: Neo | last post by:
Hello All, Although, I have read all the advantages of using Try Catch Block instead of "On error goto", I am still confused what is alternative for classic "Resume" statement. "Resume" was one...
2
by: AB | last post by:
Hi All, I've got a section of code which resembles do { try...........(1) { //do something here }
7
by: Rob R. Ainscough | last post by:
In VB6 I can use Resume Next to execute the line of coding following the line that cause an exception. There doesn't appear to be anything similiar when using Try...Catch -- so how can one resume...
3
by: E. Kwong | last post by:
I have a For loop to insert record to a SQL server database, like: For Each item As ListItem In cblxyz.Items If (item.Selected) Then .... do something Try srcxyz.Insert() Catch SQLExp As...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.