Results 1 to 8 of 8

Thread: Trap Type-Mismatch Error - Resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262

    Trap Type-Mismatch Error - Resolved

    Guys,

    I am having a problem trapping an error and VB error handling is not helping me out at all.

    I have the computer connected to a device located on the comm port. It is sending information back to the computer. Occasionally, the device sending data sends back garbage and the vb program crashes.

    I have a sub that is basically like so

    VB Code:
    1. On Error Goto errHandler
    2.  
    3. Select Case Message
    4.  
    5. Case 'blah'
    6.  
    7. Case 'blah blah'
    8.  
    9. End Select
    10.  
    11. Exit Sub
    12.  
    13. errHandler:
    14.   end sub

    It all works fine until I get an extra charicter or a bit flips over the commport. I have a line in the Case statement that looks for certain values and then converts them to normal numbers so a person can read them

    so I have this

    VB Code:
    1. IsNumeric(CInt("&H" & tempCommand))

    If tempCommand comes in like "1E" then all is good
    If it comes in like "1R", I get a type mismatch error.

    Why wont this go to the errHandler?

    Jerel
    Last edited by Phenglai; Nov 20th, 2003 at 01:38 PM.

  2. #2
    Member
    Join Date
    Oct 2003
    Posts
    40
    When you run a program within the VB6 IDE.. i.e. you are DEBUGGING it.. then errors ALWAYS cause an error message, and the choice "End " or "Debug"

    In the real program you will NOT see this message, nor the error.

    SO how do you know that the error handler is good? (how to debug an error handler?) well.. if you are single stepping your program with F8 it behaves as though it is not being debugged.. so you can see that your error code is working..

    try it!

    put a breakpoint before the code that makes an error.. send some bad characters to it.. it will break at the breakpoint, not the error.. now press F8 over and over again to step your code.. you see the error handler work

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    actually cjard that isn't strictly true... you can set how it works: right-click in a code window, select "Toggle", and you get three options:

    "Break on all errors" - act as you say (every error give a message)
    "Break in class module" - act as though the program is running (any error handlers will run)
    "Break on unhandled errors" - erm.. not entirely sure (and no help on this pc)

    from the sounds of it, setting "Break in class module" would be the best idea (or F8 to see it in action step-by-step).

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Right, this problem will occur if you have break on all errors set and are running under the debugger, but I don't believe that is the default value, and seems like a pretty weird choice to me, since it makes testing pretty bizarre.

    If you do have that option set, that's your problem. However, if you don't have it set, you might try a (slow) routine that would confirm that the number can be interpreted correctly.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Phenglai: Something else must be going on because the following does go to the error handler.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     On Error GoTo ErrorRoutine
    4.  
    5.     Const VAL As String = "1R"
    6.     If IsNumeric(CInt("&H" & VAL)) Then
    7.         MsgBox "OK"
    8.     End If
    9.  
    10.     Exit Sub
    11.  
    12. ErrorRoutine:
    13.  
    14.     MsgBox Err.Description
    15.  
    16.  
    17. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262
    Guys,

    Thanks for your help.

    Martin, I tried your code first in a seperate project just to see if it worked and it did not. It never made it to the error handler.

    So I did a little research based on what si-the-geek stated. I have changed the IDE to break on unhandled errors. This way it will only break when I an running in the IDE when there is an error and no error handler to catch it.

    I re-ran you code Martin and it worked like it should.

    I never even knew that option was there. I have been programming in VB 6.0 since 99 and never even knew it existed. Its amazing how you can work with something and still never know all of it. Anyway, I probably should have known that.

    Thanks alot guys, I will try to do some more testing to make sure my code will make the fixes now in the error handler routine.

    Thanks,

    Jerel

  7. #7
    Member
    Join Date
    Oct 2003
    Posts
    40
    Originally posted by si_the_geek
    actually cjard that isn't strictly true... you can set how it works: right-click in a code window, select "Toggle", and you get three options:

    "Break on all errors" - act as you say (every error give a message)
    "Break in class module" - act as though the program is running (any error handlers will run)
    "Break on unhandled errors" - erm.. not entirely sure (and no help on this pc)

    from the sounds of it, setting "Break in class module" would be the best idea (or F8 to see it in action step-by-step).
    thanks for the info si.. i posted the info i gave, after asking the senior vb developer here to answer the question.. no offence to the guy, but his english isnt as strong as a native person's so theres only so much detail i can put across before he gets confused: ) (he started telling me how to Shell Execute, when i first asked him if there was a difference between starting a program from within the ide and starting a program from windows..)

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    no worries folks, we all miss out something every now and then... lets face it there's lots of options to fiddle with

    I'm glad I learnt this one a long time ago!!

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