Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ON Error Resume Next Vs GoTo 0 3

Status
Not open for further replies.

Jeet2004

MIS
Jun 29, 2005
96
0
0
US
I know its a dumb question for a lot of you but frankly i dont know the difference.
I know on resume next it takes to the next line of code when error occurs and GOto 0 disables error handling but dont both mean the same thing ??
ultimately ur ignoring errors

Kindly some one explain me this ?
Thanks
 
Resume next = ignore the error.

Goto 0 = make the program show a big ugly error message.
 
At the risk of getting grumppy here, if you have VB6 try this:

Code 1:
On error resume Next
Error 5

Code 2:
On error goto 0
Error 5


Code 1 will not produce any error messages. Code 2 will pop up a big ugly error that scares users of programs.

Take my word for this I am developing a vb6 program with over 20 thousand lines of code.
 
Jeet2004,

The link you provide is for vbScript, not vb6. NewGuy is (mostly) correct in saying that On Error Goto 0 generates an Ugly error message. If the on error goto 0 is in a subroutine but the calling subroutine has error handling, then you can trap it and move on.

If you generate an error in VB that is not handled, the default error handler kicks in, displaying a vb'ish error message. This message will have an OK button. When clicked, your app will crash.

Don't beleive me? Try it for yourself. Create a new vb app. but 2 buttons and the form, and this code.

Code:
Private Sub Command1_Click()
    
    MsgBox 10 / 0
    
End Sub

Private Sub Command2_Click()
    
    On Error Resume Next
    MsgBox 10 / 0
    
End Sub
Now, from within the IDE, click tools -> Options -> General and select 'Break on unhandled Errors'. Or, if you prefer, you can compile the EXE and run it that way.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Guys,
I dont mean to sound rude or disbeliving.
I was just making sure that i am on the same page.

The reason i was asking was I have an error handling routine in vb6 and then to pass the error from subroutine to calling function i wanted to use on error goto 0 so that i can raise the error in sub and pass it to calling function(picked from a website how to do this)
Thanks for your help guys
you both earned a star each.
 
In my opinion, it is best to re-raise errors if you don't want to handle them in whatever subroutine your in.

You should look in to m-z tools. They have a utility that can automate your error handling. Personally, I don't use it because I created my own (before I learned about m-z tools). M-Z Tools is free.




-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
see CasperTFG's faq on error handling faq222-1694


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
<In my opinion, it is best to re-raise errors if you don't want to handle them in whatever subroutine your in.

Mine too.
 
Casper's faq is excellent for a foundation on how to handle errors. He doesn't mention On error goto 0, though, since it isn't considered good practice.

Simply put, on error goto 0 disables any error HANDLERS (negates any other on error statements in your code), whereas on error resume next disables any error HANDLING (continues running as if errors didn't happen).

On error resume next is often misused to get the runtime environment to ignore errors returned by an object, that don't seem to cause trouble to the application. Trouble with that is that 5 years down the line someone notices that their accounts are several million dollars off, or whatever, the code is undocumented, and the person who wrote it is now a beet gardener in Idaho.

Use On error resume next for "inline error handling." In a situation where you know that a particular code line can cause an error, such as attempting to open a file provided by the user, you can evaluate Err.Number and see if the line made an error. (The Err object is always set, whether you've set on error resume next or not. Err.Number will be 0 if there hasn't been an error.)

Code:
Public Function GetOurFile() as Boolean
dim fn as String
On Error Resume Next
fn = InputBox "Enter File:"
'fso is an open FileSystemObject--omitting code
[COLOR=blue]fso.GetFile fn 'could raise an error[/color]
select case Err.Number
   Case 0 
      GetOurFile = True
   Case 2
      MsgBox "File Not Found."
      GetOurFile = False
   Case Else
      'reraise the error if you don't handle it
End Select
Here's a bit of code that demonstrates a couple of other features of the error handler.
Code:
Public Sub Recip()
dim x as double
On Error GoTo ErrHandler
[COLOR=red]Input:[/color]
[COLOR=blue]x = InputBox("Enter a number:") 'could cause type mismatch error
msgbox "The Reciprocal of " & x & " is " & 1/x 'could cause division by zero error[/color]
Exit Sub
ErrHandler:
Select case err.number
   Case 11 'Division by zero
      Msgbox "enter a number other than Zero"
         [COLOR=red]Resume Input[/color]
   Case 13 'type Mismatch
      MsgBox "Enter a valid number"
         Resume
   case else
      Err.Raise Err.Number
End Select
End Sub
So, here, the InputBox line will raise a type mismatch error if the user inputs something that can't be evaluated as a double, so resume will send to that line. However, if the user inputs a zero, the line after the InputBox line will raise the error, and we still want to resume at the Inputbox line. That's where you can use the place marker in the Resume statement.

HTH

Bob

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top