473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 10527
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
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
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
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
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
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
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
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
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
by: Rahul | last post by:
Hi Everyone, I have the following code, int main() { bool continue1 = true; while(continue1) { try
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.