|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Exception handling, Retry, Resume Nextuser 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 > 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 Robinson wrote:
Show quoteHide quote > > How can I construct the same "Retry" exception handling behaviour in C#. That's the funniest C# code I have ever seen! ;)> > 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! ;) It was crossposted to VB.NET, what do you expect? ;)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. 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. -- Show quoteHide quoteDavid 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 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. -- Show quoteHide quoteDavid 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
SQL UPDATE query help please
How to get a form's property value from a class? Russian text output Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested How to identiy numerics in a string? PDF Creation components ListView ListItems - can they be hidden? How to uniquely identify a process? adding listbox selected items Debugging a COM Interop Class Library |
|||||||||||||||||||||||