Solved

error handling

Posted on 2004-03-25
10
285 Views
Last Modified: 2010-05-01
Dear Experts,
I copied the following code from MSDN, I suppose that On Error GoTo will avoid run-time error message box show up. But when I try to run the following code in debug mode line by line, run-time error message box still poped up, why? If error occurs in line 'Kill "myfile.txt"', why it didn't go to ErrorHandler?
Please help!

Option Explicit

Private Sub Command1_Click()
    OnErrorStatementDemo
End Sub

Sub OnErrorStatementDemo()
Dim ObjectRef
Dim Msg As String
   On Error GoTo ErrorHandler
   Open "myfile.txt" For Output As #1
   Kill "myfile.txt"
   On Error GoTo 0
   On Error Resume Next   ' Defer error trapping.
   ObjectRef = GetObject("MyWord.Basic")   ' Try to start nonexistent
            ' object, then test for
    'Check for likely Automation errors.
   If Err.Number = 440 Or Err.Number = 432 Then
      ' Tell user what happened. Then clear the Err object.
      Msg = "There was an error attempting to open the Automation object!"
      MsgBox Msg, , "Deferred Error Test"
      Err.Clear   ' Clear Err object fields
   End If
Exit Sub      ' Exit to avoid handler.
ErrorHandler:   ' Error-handling routine.
   Select Case Err.Number   ' Evaluate error number.
      Case 55   ' "File already open" error.
         Close #1   ' Close open file.
      Case Else
         ' Handle other situations here...
   End Select
   Resume   ' Resume execution at same line
            ' that caused the error.
End Sub
0
Comment
Question by:siyu
  • 3
  • 3
  • 3
10 Comments
 
LVL 5

Expert Comment

by:g0rath
ID: 10680908
because the error isn't being cleared. Look at you ErrHandler

If the error occurred in the sameprocedure as the error handler, execution resumes with the statement that caused the error. If the error occurred in a called procedure, execution resumes at thestatement that last called out of the procedure containing the error-handling routine.

So if the file doesn't exist in the first place you aren't capturing that error and are just returning without fixing it thus causing the error to occur again.
0
 
LVL 1

Author Comment

by:siyu
ID: 10681041
Thank you for your reply, but file does exist, I got run-time error 55, so it should go to ErrorHandler to fix this error, then go back to the statement that causes the problem, right? But why it doesn't? How to avoid run-time error message box pop up?
0
 
LVL 1

Author Comment

by:siyu
ID: 10681153
I just figured out that I didn't choose Tool->options..->general->Error Traping->Break on unhandled error.
After I did this, run-time error message box doesn't pop up again.
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10681193
ah ok I was wondering because I looked at it wrong, but then couldn't find out why it was doing what you said it was....it worked fine for me
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:savvage
ID: 10681452
I always use On Error Resume Next for two reasons:

Firstly: If another error occurres along the way, you can just ignore it, instead of having VB come to a complete halt. This is of course a disaster when the program's been compiled.

Secondly: You can check for errors whereever you want. So if you want VB to always run some functions after a place where an error can occur or not, just do the error check behind that.

Anyway, silly that VB wants you to set up the Error Trapping.
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10681514
personally I can't wait until I can convert my projects to .Net because I prefer the whole try { } catch thing
0
 
LVL 1

Expert Comment

by:savvage
ID: 10681721
That works quite well, too, although you can make this yourself using On Error Resume Next.

For example:

On Error Resume Next
Open "C:\file_doesnt_exist.txt" For Input As #1
Close #1
If Err.Number <> 0 Then
   Debug.Print "Error: " & Err.Number
   Err.Clear
End If

Is the same as Java's try {} catch:
try
{
   //open the file, i'm not going to type this out
}
catch (Exception e)
{
   System.out.println("Error: "+e);
}
0
 
LVL 1

Author Comment

by:siyu
ID: 10681864
Savvage,
Thank you for your comments. But by using On Error Resume Next, we only skip the error and can't fix the error(if it is fixable at run-time) and automatically go back to execute the statement that causes the error again, right?
0
 
LVL 1

Accepted Solution

by:
savvage earned 100 total points
ID: 10682263
Well, not really. On Error Resume Next just continues with the next line. It stores the error data and further ignores it.

But the whole idea behind Resume Next is that you can insert error checks wherever you like.
For example:

For n = 1 to 10
   On Error Resume Next
   ' some line that causes an error
   If Err.Number <> 0 Then
      Err.Clear
      If MsgBox("An error occurred.", vbOkCancel) = vbCancel Then Exit For
   End If
Next n

If the user presses cancel, then the loop is exited.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use closed file on desktop in vba 6 67
Excel Vlookup to move data back to source. 4 84
Using "ScreenUpdating" 6 55
Exit a vb6 apps when a calling it apps closes 15 43
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now