?
Solved

error handling

Posted on 2004-03-25
10
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 300 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses

719 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