Results 1 to 15 of 15

Thread: Is it possible - err.handler problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    Look at this code:

    Code:
    On Error GoTo err_han
       Dim DataFile as Field
       Do Until recordset1.EOF
            strPath = DataFile
                 Open strPath For Input As #1
    lab1:    recordset1.MoveNext
       Loop
    Exit Sub
    err_han:
    Select Case Err.Number
     Case Is = 53
            Label2.Caption = "No file to open"
            DoEvents
            GoTo lab1
     Case Else
          MsgBox Err.Number & " - " & Err.Description & " - " & Err.Source
    End Select
    On first execution of DO LOOP error handler traps err.number 53 (File ton found)- That OK!

    On next execution (second step) error handler does not work and I get Run time error '53'. File not found!

    Is this weird or what?
    Ermin Gutenberger

    VB.NET 2010

  2. #2
    TheSarlacc
    Guest

    i know...

    i had the same problem ages ago! it annoyed the crap outta me.

    put an On Error goto err_han after the err_han: line

    vb will produce error messages if an error occurs while inside an error handler subroutine! so u tell vb that u r not handling errors anymore, and if one happens, goto [label]. sort of like a trick

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    If I tell VB not to handle errors it my code will create run time error very often. I use error handler to trap all kinds of errors and in error handler I have code that handles that error. This code can be quite complex.

    So I can't tel VB not to handle error.
    Ermin Gutenberger

    VB.NET 2010

  4. #4
    TheSarlacc
    Guest

    sorry bad explanation

    sorry i am not good at explainin code, just givin it! hehe lol
    just do what i told ya and it will work

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Vb doesn't invoke the error handler if it thinks it is handling an error already. So an error inside the error handler will not be handled by this error handler.
    This means you need to tell vb that error handling is finished, and you resume with normal code.
    You do this with the Resume keyword.
    Replace 'GoTo lab1' with 'Resume lab1' and all should work again.

  6. #6
    TheSarlacc
    Guest
    took the words right out of my mouth

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by Frans C
    Vb doesn't invoke the error handler if it thinks it is handling an error already. So an error inside the error handler will not be handled by this error handler.
    This means you need to tell vb that error handling is finished, and you resume with normal code.
    You do this with the Resume keyword.
    Replace 'GoTo lab1' with 'Resume lab1' and all should work again.
    I tend to disagree with the former part of your opinion. I have some code in which I have created the error handler as a DLL. Every time there is an error, from my application I call the error object in my DLL. I had forgotten to create a new instance of this object in the first place. So the first time an error occured in my application, the control went inside the error handler, tried to invoke the error object in my DLL and since it had not been instantiated, fired another error, which was again trapped inside the same error handler.

    So I think even if your error handler causes any errors, they will be trapped by the same error handling code.

    And I agree with the later part, that you should use 'Resume ...' to resume control in the normal code.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    If I understood it right, the following thing happens:
    If an error occurs in a procedures error handler, the first error handler up in the call stack is invoked. If there is no such error handler, a standard error message is displayed and the program is terminated.
    The following test proves this:
    Code:
    Private Sub Command1_Click()
        Call CheckError1
    End Sub
    
    Private Sub CheckError1()
    On Error GoTo HandleIt
        Call CheckError2
        Exit Sub
    HandleIt:
        MsgBox "CheckError1"
    End Sub
    
    Private Sub CheckError2()
        Call CheckError3
    End Sub
    
    Private Sub CheckError3()
    Dim x As Double
    On Error GoTo HandleIt
        x = 1 / 0
        Exit Sub
    HandleIt:
        MsgBox "CheckError3"
        x = 1 / 0
    End Sub
    The first error occurs in procedure CheckError3, the error handler is invoked, and the messagebox says "CheckError3". Next another error is generated inside the error handler. Because the error is invoked inside the error handler of CheckError3, its error handler is not invoked, instead the error handler of CheckError2 should be invoked, but this procedure doesn't have one. One step up in the call stack is procedure CheckError1, and this his an error handler. The next messagebox says CheckError1.

    I rest my case.

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Yikes! I hate to say this, but

    YOU ARE RIGHT!

    Code:
    Option Explicit
    
    Private Sub Form_Activate()
    On Error GoTo MyError
        Err.Raise 5
        Exit Sub
    
    MyError:
        MsgBox Err.Description
        Err.Raise 13
    End Sub
    I shall seek solace in proving you right in fewer lines of code!

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Talking

    But your example doesn't show that the calling sub's error handler is used.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110

    Thanks guys. I solved my problem with

    resume lab1

    code. But then I saw that my code isn't written as it should be so I rewrite it and doesn't need error handling in that part anymore.

    But it was a good school of error handling!

    Thanks!
    Ermin Gutenberger

    VB.NET 2010

  12. #12
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by Frans C
    But your example doesn't show that the calling sub's error handler is used.
    If you try the example, the runtime error 5 is trapped in the error handler MyError. Then the error 13 is not trapped, and VB pops up its error dialog. This proves that the same error handler is not used to handle errors inside the error handling code.

    According to my argument, even for error 13, the MyError code should have been invoked. But since it does not happen so, my argument is false.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  13. #13
    New Member
    Join Date
    Mar 2001
    Location
    DFW, Texas
    Posts
    12

    Exclamation

    The error you are encountering is due to the fact you haven't closed the initial file you were trying to read.
    you opened a file within the loop but did not CLOSE it. The second pass of the loop is tryin to open another file
    to the same file handle. If you are trying to open multiple sources you will need to change the File handle to a variable that is different for each one.

    [ORIGINAL]
    On Error GoTo err_han
    Dim DataFile as Field
    Do Until recordset1.EOF
    strPath = DataFile
    Open strPath For Input As #1
    lab1: recordset1.MoveNext
    Loop

    [Option #1]
    On Error GoTo err_han
    Dim DataFile as Field
    Do Until recordset1.EOF
    strPath = DataFile
    Open strPath For Input As #1
    Close (#1)
    lab1: recordset1.MoveNext
    Loop

    [Option #2]
    [ORIGINAL]
    On Error GoTo err_han
    Dim DataFile as Field
    Do Until recordset1.EOF
    strPath = DataFile
    fhInFile = freefile
    Open strPath For Input As #(fhInFile)
    lab1: recordset1.MoveNext
    Loop


    If you are trying to determine if a file exists then try this instead.

    On Error GoTo err_han
    Dim DataFile as Field
    Do Until recordset1.EOF

    strPath = DataFile

    If len(dir(strpath)) > 0 then
    ' this is a valid file.
    else
    ' this file does NOT exist.
    end if

    lab1: recordset1.MoveNext

    Loop

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Location
    SLOVENIA, Europe
    Posts
    110
    Actually I forgot to write in my sample code

    close #1.

    A did that somewhere in the proccess, so my filenumber #1 was "free" for new open file.

    but any way VB should trap error for wrong file number but it didn't.

    That was NOT my problem.

    Code:
    resume lab1:
    solved my problem. But as I said I rewrote entire procedure so i don't need error handling anymore!
    Ermin Gutenberger

    VB.NET 2010

  15. #15
    Addicted Member
    Join Date
    Apr 2001
    Posts
    250
    Simple. If you wanna stop the file not found error, tell VB to open another file, a dummy file, and then close it. Then inform the user if it's their fault.
    Why does everyone think I may be dangerous? I'm just good at computers.

Posting Permissions

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



Click Here to Expand Forum to Full Width