Results 1 to 9 of 9

Thread: error trapping ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    Hey,
    can someone give me the simplest example of error trapping?
    if there is no simple example, no prob.
    thanks
    pnj

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Code:
    Function uh() As Boolean
    Dim i as Integer
      On Error Goto Oops
      i = 'a'
      uh = true
      exit function
    Oops:
      uh = false
    End Function

  3. #3
    Guest
    This example will create an Overflow and display a
    MessageBox telling us what happened.

    Code:
    'The VB to go to ERR_HANDLE if an Error occurs
    On Error GoTo ERR_HANDLE
        'Create an Overflow (Error 6)
        Err.Raise 6
    Exit Sub
    
    ERR_HANDLE:
    'Display a MessageBox telling us we have an Error
    If Err = 6 Then MsgBox ("Over flow, click to resume")

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537

    why?

    I am sure there is a good reason to do this but I am not yet sure why. When my programs have problems they automaticly tell me.
    but mine are not .exe so maybe that is why.
    um...don't know.
    I am geussing this is a way to track errors in a program.
    so would you say something like
    if err=6 then
    if err=4 then
    if err=3 then....ect.
    and list every error possible?
    I am new to this can you tell?
    thanks
    pnj

  5. #5
    Guest
    If you want to some a specific action when a specific error occurs, yes, you can use the above statements. Use the On Error Resume Next statement to skip the statement where the error occured and continue execution of the program.

    Code:
    'If an Error occurs, skip that statement
    'continue execution of the program
    On Error Resume Next
    'Create an Overflow (Error 6)
    Err.Raise 6
    Print "Hello"

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    you can also use "Err.Description" to display the error message.

    code:

    On Error GoTo ErrMsg
    'your code

    ErrMsg:
    Msgbox Err.Description




  7. #7
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    pnj:
    If you run a program from within VB it depends on the settings when and how an error is shown.
    But in exe's, it doesn't work that way....

    The other examples show how to trap an error.
    If you know you can expect certain errors (for instance, file does not exist, file is locked, etc), you can add to you errorhandler:
    If Err.Number = <certain number> Then
    ' action here
    ElseIf Err.Number = <other number> Then
    ' do something else
    Else
    ' display an error, shutdown windows, whatever
    End If

    That way you can do specific task when certain errors occur.
    Hope this helps

    Crazy D

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Error trapping is the weak programmers easy way out.

    Ok, that was maybe not what i meant but i thought it sounded very familiar in some way. Well but that's somehow became my motto

    Often you can avoid error handling by just using if statement to prevent the error from occuring,
    I.E:
    Code:
    If a<>0 then c=b/a
    but sometimes you have to deal with it, whenever you don't know how to prevent it:
    Code:
    On error resume next
    c=b/a
    if err=11 then err.clear:msgbox "Division by 0"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I'd Dissagree with that ked.

    I use error trapping if I'm using the same function in a huge loop where in some cases the parameters will raise an error. For example if I'm making an ActiveX control that paints itself to look like a shere, the way I do this is to go through every pixel in my grid, find out it's distance from the centre and then find the colour of that picel based on
    Code:
    Sqr((usercontrol.Width * usercontrol.Width * 4) - (R * R))
    This will raise an error if its radius is greater than usercontrol.width/2 it's quicker to set up an error trap and just skip some code if this raises an error than it would be to check it myself, especially if it's a large activeX control with lots of pixels to calculate.

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