Solved

error handling

Posted on 2004-03-25
10
291 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
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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