How to implement "On Error Resume"?

R

Rexel

Implementing VB's stock error handler, "On Error Goto Err_Label", is
easy enough using Try/Catch/Finally.

I also figured out how to implement "On Error Resume Next" case, like
when you want to delete a file but don't care about the error if the file
doesn't exists.
So:

Try
'main code

Try
'line causing error
Catch
End Try

'continue main code

Catch
'main error handler
Finally
'cleanup if needed
End Try


The simple Try/Catch block in the middle will do the same as
"On Error Resume Next".

But I haven't been able to implement "On Error Resume", where I
want to try again the line of code that caused the error.
It seems that you have to set up a loop enclosing the Try/Catch block.

How do the experts implement "On Error Resume" case in VB.NET?
 
D

Dominique Vandensteen

I can't image why you would do that...
But here some code

dim maxretries as Integer = 5
dim counter as Integer = 0
dim succes as Boolean = False

while (not succes and (counter < maxretries))
try
<do stuff here>
succes = True
catch ...
end try
counter += 1
end while


hope this helps

dominique
 
H

Herfried K. Wagner [MVP]

* Rexel said:
Implementing VB's stock error handler, "On Error Goto Err_Label", is
easy enough using Try/Catch/Finally.

I also figured out how to implement "On Error Resume Next" case, like
when you want to delete a file but don't care about the error if the file
doesn't exists.
So:

Try
'main code

Try
'line causing error
Catch
End Try

'continue main code

Catch
'main error handler
Finally
'cleanup if needed
End Try


The simple Try/Catch block in the middle will do the same as
"On Error Resume Next".

But I haven't been able to implement "On Error Resume", where I
want to try again the line of code that caused the error.
It seems that you have to set up a loop enclosing the Try/Catch block.

How do the experts implement "On Error Resume" case in VB.NET?

Why not use unstructured error handling in this case? It will make
everything easier...

;-)
 
W

William Ryan

You can use old school error handling using the Err object or you can use
Structured Exception handling, but you can't mix in the middle of a
structured block. You can rip out the try catch and just use OnError Resume
Next or you can call a goto statement in the catch block. However,
structured exception handling is vastly superior to the old methods and you
should avoid it if at all possible.
 
R

Rexel

You can use old school error handling using the Err object or you can use
Structured Exception handling, but you can't mix in the middle of a
structured block. You can rip out the try catch and just use OnError Resume
Next or you can call a goto statement in the catch block. However,
structured exception handling is vastly superior to the old methods and you
should avoid it if at all possible.

Yes, I would like to use the structured method and avoid the use of On
Error, or Goto etc.
Maybe I didn't make myself clear. I want to implement the functionality
of "On Error Resume", but using only the new structured method.

The reply from Dominique using a loop seems to do the trick.
 
R

Rexel

I can't image why you would do that...
But here some code

dim maxretries as Integer = 5
dim counter as Integer = 0
dim succes as Boolean = False

while (not succes and (counter < maxretries))
try
<do stuff here>
succes = True
catch ...
end try
counter += 1
end while


hope this helps

dominique

Yes, that will do. Thanks. I kind of suspected I had to use a loop.

As to why I would do that, well, in VB we use "On Error Resume" frequently.
One example is when the line causing error is a SQL operation, and the
cause of error is a deadlock. So in this case we try the line agian via On
Error Resume mechanism, after a short delay. Of course there is a counter
for how many trials before giving up.

I am learning VB.NET, and as I go along I try to figure out how to do in
VB.NET the things I used to do in VB, without using any old VB stuff.
 
O

Ot

I'm no "expert" but...

Since I haven't programmed in VB6, I am unsure what it does, but assuming
that it simply retries a particular statement until it finally works.
(Aren't you risking an infinite loop? Seems bad practice.)

dim done as boolean = false
while not done
try
somefunction()
catch ex as system.exception
if ex is the expected one then
'do nothing
else
'handle the error
done = true
end if
end try
end while
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top