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

Exception handling, Retry, Resume Next

P: n/a
There are situations where you want to retry/rerun the same code again (if
user wants) for unlimited times for a particular exception thrown. For
example in situations like there's no CD in the drive or some file is locked
by another process you want to display message dialog to user with "Retry"
and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and
if user clicks "Retry" you want to run the same code again that thrown the
exception.

How can I construct the same "Retry" exception handling behaviour in C#.

Secondly, how can I construct VB6's "OnError ResumeNext" equivalent in C#
for particular exception handling. "OnError ResumeNext" actually resumes with
next instruction/code if a line/code produces an exception. For example in a
loop if something raised exception I want loop to continue with next
iteration.

How can the VB6's "OnError ResumeNext" constructed in C#.

regards,
Adil
Nov 22 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Adil Akram wrote:
There are situations where you want to retry/rerun the same code again (if
user wants) for unlimited times for a particular exception thrown. For
example in situations like there's no CD in the drive or some file is locked
by another process you want to display message dialog to user with "Retry"
and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and
if user clicks "Retry" you want to run the same code again that thrown the
exception.

How can I construct the same "Retry" exception handling behaviour in C#.
You build a loop construct with a /trailing/ condition that attempts the
action and exist if (a) it was successful or (b) the user chooses to
give up.
Secondly, how can I construct VB6's "OnError ResumeNext" equivalent in C#
try
{
Statement1;
}
finally {}
try
{
Statement2;
}
finally {}

.... etc. ...
For example in a
loop if something raised exception I want loop to continue with next
iteration.
Forgive me but that's not quite the same thing.

"On Error Resume Next" simply ploughs through the code, ignoring any
errors/Exceptions along the way and relying on the following code to
test for the errant condition.

What you describe is something like this

for ( ; ; )
{
try
{
Statement2;
}
catch( Exception e )
{
continue;
}
}

Regards,
Phill W.
Nov 22 '06 #2

P: n/a
How can I construct the same "Retry" exception handling behaviour in C#.
This is quite simple:

Dim bTrying As Boolean = True, nTried As Integer = 0
While bTrying And nTried < 3

Try
.... do something

Catch Ex As Exception

If .... Fatal error? (don't retry) Then
bTrying = False
End If
' Reset any data structures we modified during our try...

....

' Incremenet try count

nTried += 1

End Try

End While


Nov 22 '06 #3

P: n/a
Adil,

Usually when something needs to be retried over and over it means that
"something" needs to be it's own thing (whether that be an object or
function) that returns success or failure ... then the caller can retry
x number of times as needed.

Thus the error handling remains clean in the "something" and the caller
remains clean as well.

Nov 22 '06 #4

P: n/a
Robinson wrote:
How can I construct the same "Retry" exception handling behaviour in C#.

This is quite simple:

Dim bTrying As Boolean = True, nTried As Integer = 0
While bTrying And nTried < 3

Try
.... do something

Catch Ex As Exception

If .... Fatal error? (don't retry) Then
bTrying = False
End If
' Reset any data structures we modified during our try...

....

' Incremenet try count

nTried += 1

End Try

End While
That's the funniest C# code I have ever seen! ;)

Nov 22 '06 #5

P: n/a
That's the funniest C# code I have ever seen! ;)
It was crossposted to VB.NET, what do you expect? ;)
Nov 22 '06 #6

P: n/a
Actually, the equivalent to On Error Resume Next is:
try
{
//one line of code
}
catch
{
//do nothing
}
etc. for each line of code.
Your try/finally version will not quell the exceptions.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Phill W." wrote:
Adil Akram wrote:
There are situations where you want to retry/rerun the same code again (if
user wants) for unlimited times for a particular exception thrown. For
example in situations like there's no CD in the drive or some file is locked
by another process you want to display message dialog to user with "Retry"
and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and
if user clicks "Retry" you want to run the same code again that thrown the
exception.

How can I construct the same "Retry" exception handling behaviour in C#.

You build a loop construct with a /trailing/ condition that attempts the
action and exist if (a) it was successful or (b) the user chooses to
give up.
Secondly, how can I construct VB6's "OnError ResumeNext" equivalent in C#

try
{
Statement1;
}
finally {}
try
{
Statement2;
}
finally {}

.... etc. ...
For example in a
loop if something raised exception I want loop to continue with next
iteration.

Forgive me but that's not quite the same thing.

"On Error Resume Next" simply ploughs through the code, ignoring any
errors/Exceptions along the way and relying on the following code to
test for the errant condition.

What you describe is something like this

for ( ; ; )
{
try
{
Statement2;
}
catch( Exception e )
{
continue;
}
}

Regards,
Phill W.
Nov 22 '06 #7

P: n/a
try
{
//one line of code
}
catch
{
//do nothing
}
etc. for *every* line of code.
As you can see, you probably want to rethink whether you still want to
simulate this VB construct.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Adil Akram" wrote:
There are situations where you want to retry/rerun the same code again (if
user wants) for unlimited times for a particular exception thrown. For
example in situations like there's no CD in the drive or some file is locked
by another process you want to display message dialog to user with "Retry"
and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and
if user clicks "Retry" you want to run the same code again that thrown the
exception.

How can I construct the same "Retry" exception handling behaviour in C#.

Secondly, how can I construct VB6's "OnError ResumeNext" equivalent in C#
for particular exception handling. "OnError ResumeNext" actually resumes with
next instruction/code if a line/code produces an exception. For example in a
loop if something raised exception I want loop to continue with next
iteration.

How can the VB6's "OnError ResumeNext" constructed in C#.

regards,
Adil
Nov 22 '06 #8

P: n/a
Sorry - I realized that I didn't indicate which question I was answering.
The code I posted was an equivalent for On Error Resume Next.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"David Anton" wrote:
try
{
//one line of code
}
catch
{
//do nothing
}
etc. for *every* line of code.
As you can see, you probably want to rethink whether you still want to
simulate this VB construct.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"Adil Akram" wrote:
There are situations where you want to retry/rerun the same code again (if
user wants) for unlimited times for a particular exception thrown. For
example in situations like there's no CD in the drive or some file is locked
by another process you want to display message dialog to user with "Retry"
and "Cancel" buttons (As Windows shows "Retry" option if no CD in drive) and
if user clicks "Retry" you want to run the same code again that thrown the
exception.

How can I construct the same "Retry" exception handling behaviour in C#.

Secondly, how can I construct VB6's "OnError ResumeNext" equivalent in C#
for particular exception handling. "OnError ResumeNext" actually resumes with
next instruction/code if a line/code produces an exception. For example in a
loop if something raised exception I want loop to continue with next
iteration.

How can the VB6's "OnError ResumeNext" constructed in C#.

regards,
Adil
Nov 22 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.