Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    On Error fails to catch

    I'm sure there must be a way to "catch" all errors in a loop. What is it?

    I'm not a big fan of OnError, but I'll use it to get over a very transient hurdle, as in the case below where I want to list/print/debug.print details of file converters.

    The OnError seems to catch the first error, but not subsequent errors. I r/c and set the next statement manually to force the list out.

    Placing a second "On Error GoTo Dummy" doesn't appear to re-start the error detection.




    <pre>Sub ListAllFormatConverters()
    Dim fc
    Open "d:temperase.txt" For Output As 1
    For Each fc In FileConverters
    On Error GoTo Dummy
    Write #1, fc.OpenFormat; fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    GoTo NextFc
    Dummy:
    Write #1, _
    "No registered converter supports reading files in this format.";_
    fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    NextFc:
    On Error GoTo Dummy
    Next fc
    On Error GoTo 0
    End Sub
    </pre>


  2. #2
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Kortrijk, Belgium
    Posts
    571
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error fails to catch

    I think you have to solve the problem first, before the next error can be trapped. In the example below you first get an error because the logarithm of zero does not exist, so you solve it by making b = -13 and then use the command RESUME to make the program continue where it was interrupted. The second error is because b was negative. Again you first have to solve the problem before you can continue and the errorhandling is on again.

    Sub test()
    Dim i As Integer
    Dim a As Single, b As Single
    On Error GoTo errorhandler
    a = 5
    b = 0
    For i = 1 To 10
    a = Log([img]/forums/images/smilies/cool.gif[/img]
    Next i
    Exit Sub
    errorhandler:
    If b = 0 Then
    MsgBox "The logartihm of zero does not exist"
    b = -13
    Resume
    End If
    If b < 0 Then
    MsgBox "The logarithm of a negative value does not exist"
    b = Abs([img]/forums/images/smilies/cool.gif[/img]
    Resume
    End If
    End Sub

  3. #3
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Kortrijk, Belgium
    Posts
    571
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error fails to catch

    Another possibility would be to ignore the error but write a message to your file each time an error occurs. You could use code very similar to this:

    Sub test()
    Dim i As Integer
    Dim a As Single, b As Single
    On Error Resume Next
    a = 5
    b = 0
    For i = 1 To 10
    a = Log([img]/forums/images/smilies/cool.gif[/img]
    If Err.Number <> 0 Then
    MsgBox Err & ": unexpected error"
    End If
    Next i
    End Sub

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: On Error fails to catch

    Hans, thanks for the response. Your first solution seemed to be more suitable, so I placed a RESUME statement immediately after writing the error-message to the file.

    This (RESUME) caused the control to be passed back to the statement that caused the original problem.

    Then I made it RESUME NEXT (as shown in the example below) and this time it seems to do what I want.

    I now get a report on each converter, and can make decisions accordingly


    Thanks for the help!


    <pre> Dim fc
    For Each fc In FileConverters
    On Error GoTo Dummy
    Debug.Print fc.OpenFormat; fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    GoTo NextFc
    Dummy:
    Debug.Print "No registered converter"; fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    Resume Next
    NextFc:
    Next fc
    On Error GoTo 0

    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •