473,671 Members | 2,467 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 2550
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
2870
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
1647
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 log message return from function call but excute the next instruction in the current function(when i return i want to go to the next instruction,not the instruction that was raised an exception)
3
1726
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 continue at the next Catch block up the stack. When this happens will the 'Finally' block of the original 'Try/Catch' be executed? 2. If I wish to recover from the error that caused the Exception can I put the Try/Catch in a loop so that I can...
4
2033
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 like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
3
8170
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 add an exception log to the application. So, I added an Exception handling routine that basically has a running string that adds errors to the string if they happen. I have three questions: 1. If a SQL exception happens I would like it to...
10
2283
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 written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on that ID, otherwise it returns false. **I am not looking for comments on the usefulness of this function - it is
8
2027
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 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...
41
3051
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 handle all of them. I don't care about which one happened, except to write out exception.Message to a log file. It seems verbose to write out three handlers that all do the same thing. So, I could just catch Exception. But, is that...
4
1878
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
8401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.