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 Chris Miller 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

Status
Not open for further replies.

campbere

Technical User
Oct 10, 2000
146
US
I am new to VB 6.0 so I apologize if this is a stupid question. I am supporting an application that another developer wrote, (and is no longer with the company) and throughout the code at the begining of functions I see the line:

On Error Resume Next

Can someone explain this? I have heard about error handling but I don't know how this fits in. Is this good coding practice? [sig][/sig]
 
This bascially means that if an error occurs, the program will go to the next line in the program.

At any point within a function, you can insert an On Error statement, followed by Goto <Section> or Resume Next.

If an error occurs at any point (within the function scope) after this statement, program flow goes to the specified section, or continues onto the next line, as if nothing happened.
There are a few other options with this statement, but these are the most common uses of the statement.

Most often, the On Error Resume Next statement is used because the developer will check/trap for errors somewhere else in the function. Care must always be taken when using this statement, because program functionality can be seriously affected by an ignored error.

Hope this helps.

Steve [sig][/sig]
 
Just my humble opinion but On Error Resume Next is widely held as a rather poor programming practice. Generally, one wants the On Error statement to point directly to code that can trap the error, inform the user (if necessary) and take appropriate steps to correct the error or &quot;retry&quot; to insure the validity of data later in the program.

If you want to see the types of errors the functions are generating, just remove the On Error lines and run the program. A verifiable need for this command is very rare. It appears to exist for programmers who are too busy to write their code correctly.
[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br><i>"I sure hope I'm retired when it comes time to fix all the the Year-2000 problems in our systems!"</i><br>
<b>Edward Yourdon, <u>Time Bomb 2000</u></b>[/sig]
 
hehe...... On Error Resume Next

The ultimate fix ALL!! [sig][/sig]
 
As Alt255 posted, you should use error trapping in your code. The Resume Next statment can be used to trap errors on the spot. As an example, I want to make sure that files are not open if the application is already running. The GetObject will return an error if the application is not running, an acceptable scenario. When the check is complete, it will forward all errors back to the error handling code.


On Error Resume Next

Set hApp = GetObject(, &quot;SldWorks.Application&quot;)

'Only notify if there are models/part open in SolidWorks
sTmp = hApp.ActiveDoc.GetTitle

If Err.Number = 0 And sTmp <> &quot;&quot; Then
MsgBox &quot;Please Close All SolidWorks Models Before Running This Application&quot;
End
Else
Err.Clear ' Clear Err object
End If

On Error GoTo ErrHndlr
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top