473,803 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2358
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.Overflow Exception

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

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******** ******@TK2MSFTN GP12.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:

numberOfFailure s = 0
While (there are more employees to process)
Try
ProcessNextEmpl oyee
Catch
SendErrorEmail
numberOfFailure s += 1
End Try

IF numberOfFailure s >= 3
SendErrorEmail
Exit While
End If
End While

"Joe Rigley" <jc******@spart anmotors.com> wrote in message
news:ug******** ******@TK2MSFTN GP12.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 numberOfFailure s as integer
While (there are more employees to process)
If (there is after testing something wrong) then
SendErrorEmail
numberOfFailure s += 1
if numberOfFailure s > 2 then
SendErrorEmailP lus
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 numberOfFailure s >= 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******** ******@TK2MSFTN GP11.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:

numberOfFailure s = 0
While (there are more employees to process)
Try
ProcessNextEmpl oyee
Catch
SendErrorEmail
numberOfFailure s += 1
End Try

IF numberOfFailure s >= 3
SendErrorEmail
Exit While
End If
End While

"Joe Rigley" <jc******@spart anmotors.com> wrote in message
news:ug******** ******@TK2MSFTN GP12.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******@spart anmotors.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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 numberOfFailure s >= 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******** ******@TK2MSFTN GP11.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:

numberOfFailure s = 0
While (there are more employees to process)
Try
ProcessNextEmpl oyee
Catch
SendErrorEmail
numberOfFailure s += 1
End Try

IF numberOfFailure s >= 3
SendErrorEmail
Exit While
End If
End While

"Joe Rigley" <jc******@spart anmotors.com> wrote in message
news:ug******** ******@TK2MSFTN GP12.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.Excepti on ex)
{
exCount++;
Emailer.SendErr orNotice(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******** ******@TK2MSFTN GP11.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:

numberOfFailure s = 0
While (there are more employees to process)
Try
ProcessNextEmpl oyee
Catch
SendErrorEmail
numberOfFailure s += 1
End Try

IF numberOfFailure s >= 3
SendErrorEmail
Exit While
End If
End While

"Joe Rigley" <jc******@spart anmotors.com> wrote in message
news:ug******** ******@TK2MSFTN GP12.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
6813
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 you are debugging within IDE...? Here is an example from VB: Private SomeFunction () On Error GoTo ErrHandler: MyMethod this, that
5
14080
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 statement and then I want to resume at either the same statement that triggered the error or the following statement. Perhaps I should not be using Try...Catch approach? Any suggestions? Thanks, Rob
9
275
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 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...
4
1853
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 HERRHANDLER" and the herrhandler label at the botton where i write in a logfile the name of the routine, the errors and other information Well setting myerrhandle to true seems that the error handling keeps on working in ..net is it right? what's the...
15
11775
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 of the crucial line in debugging which let VB programmers go to exact line where error was thrown. I still use on error goto instead of try catch since, I want "Resume" for debugging. Is there any alternative like Resume in VB.net for Try
2
4666
by: AB | last post by:
Hi All, I've got a section of code which resembles do { try...........(1) { //do something here }
7
6495
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 the next statement when an error can be ignored? Thanks, Rob.
3
2283
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 SqlException ....display error
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.