Solved

error handling

Posted on 2004-03-25
10
281 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Expert Comment

by:savvage
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

8 Experts available now in Live!

Get 1:1 Help Now