Exception handling, Retry, Resume Next

G

Guest

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
 
P

Phill W.

Adil said:
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.
 
R

Robinson

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
 
D

davewilliamson55555

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.
 
C

Chris Dunaway

Robinson said:
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! ;)
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top