473,513 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception handling, Retry, Resume Next

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
6 2541
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 #2
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 #3
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 #4
That's the funniest C# code I have ever seen! ;)
It was crossposted to VB.NET, what do you expect? ;)
Nov 22 '06 #5
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 #6
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 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2837
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
1640
by: vkreddy_in | last post by:
HI Currently i am handling excpetion using TRY/catch in my code.I am looking for solution some thing like this. if an excrption is occured then 1.call a CALLBACK function in a DLL 2.print the...
3
1717
by: gemel | last post by:
I am fairly comfortable with much of the .NET Exception mechanism, but I'm a bit unsure of some areas. 1. When an exception occurs and the exception is not caught then Exception processing will...
4
2022
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
3
8148
by: RSH | last post by:
Hi all, I have a situation where I am looping through a DataTable and extracting values from that DataTable and inserting them into a SQL database. The application works fine but now I want to...
10
2268
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
8
2017
by: Adil Akram | last post by:
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...
41
3022
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
4
1867
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.