473,585 Members | 2,513 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
8 2024
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#.
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.
Nov 22 '06 #2
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 #3
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 #4
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 #5
That's the funniest C# code I have ever seen! ;)
It was crossposted to VB.NET, what do you expect? ;)
Nov 22 '06 #6
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
"Phill W." wrote:
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#.

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.
Nov 22 '06 #7
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 #8
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 #9

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

Similar topics

11
2857
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
1642
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...
3
1724
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...
4
2030
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
8157
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...
10
2277
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...
8
10563
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"...
41
3039
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...
4
1874
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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...
0
8199
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. ...
1
5710
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...
0
5389
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
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 we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
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...

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.