By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,723 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

while loops and exceptions

P: n/a
Hi

I just want to clear something up in my head with while loops and
exceptions. I'm sure this will probably be a no brainer for most.

Check this simple pseudo-code out (vb.net):

------------------------------

try
While DRemail.Read
sendmail(DRemail("EMail")) 'Sends an email.
End While
Catch ex As Exception
logexception 'Logs the exception in a log somewhere
end try

------------------------------

Let's say this code iterates through a load of valid email addresses
and send out messages one by one.
Then it comes across a badly formatted email address such as
"fred@@@@flintstone..com", thus an exception will be captured and
logged.

Q - What happens after that exception is logged?

a) Does the while/end while loop abort completely? (not what I want)

b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)

c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
(desirable, if this is not the case what do I need to do?).

Many Thanks...!

Alex

May 21 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Alex wrote:
try
While DRemail.Read
sendmail(DRemail("EMail")) 'Sends an email.
End While
Catch ex As Exception
logexception 'Logs the exception in a log somewhere
end try
Q - What happens after that exception is logged?
Somewhere in the depths of the sendmail method, an error happens.

An Exception object is created and the CLR then goes looking, back up
the call stack, for an Exception handler to deal with it.

Your "Catch ex as Exception" will do nicely.

Execution continues into the Catch block, where you log the exception.

You don't have a Finally block, so, once the Catch block is done and
because you haven't re-thrown the Exception, execution continues with
the statement /after/ the "End Try".

So ...
a) Does the while/end while loop abort completely? (not what I want)
Yes, because you catch the Exception /outside/ the loop.
b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)
Of course not. The Exception goes straight out of the loop.
c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
(desirable, if this is not the case what do I need to do?).
Not the way it's coded, no. You'd have to change it to

Do While DRemail.Read()
Try
sendmail(DRemail("EMail"))
Catch ex As Exception
logexception
End Try
End Do

Be warned, though - if you have /lots/ of dodgy email addresses or, say,
network problems, your code will spend more time building and throwing
Exception objects than it will actually sending stuff and your machine
will be absolutely /hammered/ while it's doing so.

HTH,
Phill W.
May 21 '07 #2

P: n/a
From the try/catch documentation
(http://msdn2.microsoft.com/en-us/lib...tz(VS.80).aspx) :
"If an error occurs in the Try block, program control is passed to the
appropriate Catch statement for disposition."

So just embed those instructions you need inside the try/catch block
(basically just the sendmail line). Actually my personal preference in this
particular case would be likely to test before hand that the mail is valid
rather than to try and catch a possible exception. See :
http://msdn2.microsoft.com/en-us/lib...ts(VS.71).aspx

I would add "create first a global exception handler" so that you don't have
to add here and there code that just logs the exception without doing
anything else. You can then add local exception handlers if you could do
something about the error or needs to perform some cleanup rather than just
terminating...

Hopefully this is for a mailing list and not for spamming ;-)
---
Patrice

"Alex" <po******@alexshirley.coma écrit dans le message de news:
11**********************@b40g2000prd.googlegroups. com...
Hi

I just want to clear something up in my head with while loops and
exceptions. I'm sure this will probably be a no brainer for most.

Check this simple pseudo-code out (vb.net):

------------------------------

try
While DRemail.Read
sendmail(DRemail("EMail")) 'Sends an email.
End While
Catch ex As Exception
logexception 'Logs the exception in a log somewhere
end try

------------------------------

Let's say this code iterates through a load of valid email addresses
and send out messages one by one.
Then it comes across a badly formatted email address such as
"fred@@@@flintstone..com", thus an exception will be captured and
logged.

Q - What happens after that exception is logged?

a) Does the while/end while loop abort completely? (not what I want)

b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)

c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
(desirable, if this is not the case what do I need to do?).

Many Thanks...!

Alex

May 21 '07 #3

P: n/a
On May 21, 7:33 am, Alex <posti...@alexshirley.comwrote:
Hi

I just want to clear something up in my head with while loops and
exceptions. I'm sure this will probably be a no brainer for most.

Check this simple pseudo-code out (vb.net):

------------------------------

try
While DRemail.Read
sendmail(DRemail("EMail")) 'Sends an email.
End While
Catch ex As Exception
logexception 'Logs the exception in a log somewhere
end try

------------------------------

Let's say this code iterates through a load of valid email addresses
and send out messages one by one.
Then it comes across a badly formatted email address such as
"fred@@@@flintstone..com", thus an exception will be captured and
logged.

Q - What happens after that exception is logged?

a) Does the while/end while loop abort completely? (not what I want)

b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)

c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
(desirable, if this is not the case what do I need to do?).

Many Thanks...!

Alex
No offense, but this is one of types of posts that I hate. You want to
figure out what will happen in some pseudocode - particularly a thrown
exception in a loop. So why not code a simple test and check it out
for yourself (after all VS comes with a debugger so use it!) For
example create the following console project and press F8 to begin
stepping through the code - it will show you what would happen:

Module Module1

Sub Main()

Try
While True
Throw New Exception
End While
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

End Sub

End Module

But, since you asked, here's some answers:
Q - What happens after that exception is logged?
It will exit the try statement and not continue the loop.
b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)
No
a) Does the while/end while loop abort completely? (not what I want)
Yes
c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
No
(desirable, if this is not the case what do I need to do?).
Redo the loop with the try...catch inside the loop:

while DBRemail.Read()
try
sendmail(DRemail("EMail")) 'Sends an email.
catch ex as exception
' Log exception
continue
end try
end while
Thanks,

Seth Rowe

May 21 '07 #4

P: n/a
Thanks guys!
Hopefully this is for a mailing list and not for spamming ;-) Patrice
I'll add you to my email address of 600,000,000,000 satisified vi*gra
customers LOL :).

.....nope I'm definately not a spamming scumbag (give me one and I'll
skin him alive, I go through enough of their junk :)..

thanks for your help! (hope I don't get mailbombed after this :).

Alex

May 21 '07 #5

P: n/a
cj
He might not know about using throw new exception to test an exception
being thrown. If he doesn't it's hard to test. Otherwise I bet you
best answered the question by simply noting the try catch should be
around sendmail only and not the while loop if he wishes to continue
trying to send emails.

rowe_newsgroups wrote:
On May 21, 7:33 am, Alex <posti...@alexshirley.comwrote:
>Hi

I just want to clear something up in my head with while loops and
exceptions. I'm sure this will probably be a no brainer for most.

Check this simple pseudo-code out (vb.net):

------------------------------

try
While DRemail.Read
sendmail(DRemail("EMail")) 'Sends an email.
End While
Catch ex As Exception
logexception 'Logs the exception in a log somewhere
end try

------------------------------

Let's say this code iterates through a load of valid email addresses
and send out messages one by one.
Then it comes across a badly formatted email address such as
"fred@@@@flintstone..com", thus an exception will be captured and
logged.

Q - What happens after that exception is logged?

a) Does the while/end while loop abort completely? (not what I want)

b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)

c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one
(desirable, if this is not the case what do I need to do?).

Many Thanks...!

Alex

No offense, but this is one of types of posts that I hate. You want to
figure out what will happen in some pseudocode - particularly a thrown
exception in a loop. So why not code a simple test and check it out
for yourself (after all VS comes with a debugger so use it!) For
example create the following console project and press F8 to begin
stepping through the code - it will show you what would happen:

Module Module1

Sub Main()

Try
While True
Throw New Exception
End While
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

End Sub

End Module

But, since you asked, here's some answers:
>Q - What happens after that exception is logged?

It will exit the try statement and not continue the loop.
>b) Will it continue to try to send to the same dodgy email address in
an endless loop? (not good)

No
>a) Does the while/end while loop abort completely? (not what I want)

Yes
>c) Will the while/end while loop be allowed to continue (jump over the
dodgy email address) and send out the rest of the emails one by one

No
>(desirable, if this is not the case what do I need to do?).

Redo the loop with the try...catch inside the loop:

while DBRemail.Read()
try
sendmail(DRemail("EMail")) 'Sends an email.
catch ex as exception
' Log exception
continue
end try
end while
Thanks,

Seth Rowe
May 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.