Home All Groups Group Topic Archive Search About

Exception handling, Retry, Resume Next

Author
22 Nov 2006 11:11 AM
Adil Akram
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

Author
22 Nov 2006 12:04 PM
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
Author
22 Nov 2006 1:58 PM
Chris Dunaway
Robinson wrote:
Show quoteHide quote
> > 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! ;)
Author
22 Nov 2006 2:13 PM
Robinson
> That's the funniest C# code I have ever seen! ;)

It was crossposted to VB.NET, what do you expect? ;)
Author
22 Nov 2006 1:34 PM
davewilliamson55555@hotmail.com
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.
Author
22 Nov 2006 4:48 PM
David Anton
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


Show quoteHide quote
"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
Author
22 Nov 2006 6:58 PM
David Anton
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


Show quoteHide quote
"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