Results 1 to 7 of 7

Thread: [URGENT] Declaring an error handler like this... simple, but can someone help?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    101

    Question [URGENT] Declaring an error handler like this... simple, but can someone help?

    I want to declare an error handler like this if possible...

    ErrorHandler:

    Case err.number whatever
    If err.description "whateverdescription" Then
    MsgBox "do whateverdescription needs"
    Else: MsgBox "a different error message"

    Case err.number whateverelse
    Msgbox "message for whatever else"

    Case err.number yetanother
    Msgbox "message for yetanother"

    Case else
    Msgbox "error handler to catch all other others not accounted for"



    Can someone help me write it?

    I've been trying it, but i keep either getting errors (like End Select declared in wrong place), or jumping errors and always going to the last one for some reason...


    If anyone can please lend some help, please do.

    Many thanks.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    VB Code:
    1. ErrorHandler:
    2.  
    3. Select Case err.number
    4.      Case whatever
    5.            If err.description = "whateverdescription" Then
    6.                 MsgBox "do whateverdescription needs"
    7.            Else
    8.                 MsgBox "a different error message"
    9.            End If
    10.  
    11.       Case whateverelse
    12.            Msgbox "message for whatever else"
    13.  
    14.       Case yetanother
    15.            Msgbox " message for yetanother"
    16.  
    17.       Case else
    18.            Msgbox "error handler to catch all other others not accounted for"
    19. End Select

  3. #3
    Junior Member
    Join Date
    Apr 2004
    Location
    India
    Posts
    16
    Here i have shown a example where for invalid data a error
    message will be poped

    Option Explicit

    Dim i As Integer
    Dim k As Integer

    Private Sub cmderrorhandler()
    On Error GoTo Errorhandler
    i = Val(txtnumber.Text)
    k = 50 / Val(txtnumber.Text)
    Errorhandler:
    Select Case Err.Number
    Case 6
    If Err.Description = "Overflow" Then
    MsgBox "Enter a number less than 32567"
    Else
    MsgBox "something"
    End If
    Case 11
    If Err.Description = "Division by zero" Then
    MsgBox "Enter a number other than zero"
    Else
    MsgBox "something"
    End If
    Case Else
    MsgBox "Cannot find out the error"
    End Select
    End Sub
    cliff

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    What you have asked for is a very basic error handler that most people use in their apps:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const ERR_BUSY As Long = 2342
    4. Private Const ERR_FILE_NOT_FOUND As Long = 23
    5. Private Const ERR_NO_PERMISSIONS As Long = 56
    6.  
    7. Private Sub DoSomething()
    8. On Error Goto ErrHandler
    9.    'code here
    10.    Exit Sub
    11. ErrHandler:
    12.    Select Case Err.Number
    13.       Case ERR_BUSY
    14.          If MsgBox("Server Busy. Try again?", vbExclamation + vbYesNo) = vbYes Then
    15.             Resume
    16.          End If
    17.       Case ERR_FILE_NOT_FOUND
    18.          MsgBox "File not found. Please select a valid file.", vbExclamation
    19.       Case ERR_NO_PERMISSIONS
    20.          MsgBox "Permission denied. Please contact sys admin.", vbExclamation
    21.       Case Else
    22.          MsgBox "Unknown Error", vbCritical
    23.    End Select
    24. End Sub

    Woka

  5. #5

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [URGENT] Declaring an error handler like this... simple, but can someone help?

    Originally posted by VBKid04
    I want to declare an error handler like this if possible...

    ErrorHandler:

    Case err.number whatever
    If err.description "whateverdescription" Then
    MsgBox "do whateverdescription needs"
    Else: MsgBox "a different error message"

    Case err.number whateverelse
    Msgbox "message for whatever else"

    Case err.number yetanother
    Msgbox "message for yetanother"

    Case else
    Msgbox "error handler to catch all other others not accounted for"



    Can someone help me write it?

    I've been trying it, but i keep either getting errors (like End Select declared in wrong place), or jumping errors and always going to the last one for some reason...


    If anyone can please lend some help, please do.

    Many thanks.
    Why are you asking the same question in multiple places?

  7. #7
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Here is some greate error handler code.
    Not only can you display all your error messages in a custom screen, but it also records the entire route that the error was transmitted through and allows the user to email a helpdesk.

    VB Code:
    1. Private Sub Command1_Click
    2. On Error Goto ErrHandler  
    3.    Call Woof
    4.    Exit Sub
    5. ErrHandler:
    6.    MsgBox Err.Description
    7. End Sub
    8.  
    9. Private Sub Woof()
    10.    Call Growl
    11. End Sub
    12.  
    13. Private Sub Growl()
    14.    MsgBox 1/0 'force error
    15. End Sub
    With the above you get an error when you click the command button. As a developer this isn't much use as with a very large app this error could have been generated by 1 of 200 subs/functions.
    The error handler zipped up here would display the following error:
    Code:
    ORIGINATED: 
    cmdCommand1_Click
    
    HISTORY:
    Woof
     Growl
    This means it's easy to pin point exactly which function caused the error.
    Hope this helps.

    Woka
    Attached Files Attached Files

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