Results 1 to 6 of 6

Thread: Get name of funcion when error ocurr...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2003
    Location
    Brazil
    Posts
    65

    Question Get name of funcion when error ocurr...

    Hello all!

    How to i cam automatic get the original name of a function when an error occurs, and how to i cam handle one global procedure to error treatment?

    see this code:

    VB Code:
    1. Private Sub MyFunction()
    2. On Error GoTo GlobalErrorTreat:
    3.  
    4. Dim x As Integer
    5. Dim a As String
    6. Dim r As Boolean
    7.     a = "a"
    8.     x = 1
    9.     r = a * x ' this generate an error
    10.     MsgBox r
    11. End Sub
    12.  
    13. ' in a global module or class
    14. On Error GoTo GlobalErrorTreat:
    15. MsgBox "This error ocurr in : " & FunctionName
    16. 'how to get FunctionName is my problem...
    17. Resume Next

    Thanks!

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Well, there is no automatic way to get the function name, you'll need to keep track yourself.

    Also, there is no such thing as global error handling in VB, you need to set error traps in each sub/function.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    This will solve all of your error handling needs:

    Go download this: http://www.mztools.com/

    Put this:
    VB Code:
    1. On Error GoTo {PROCEDURE_NAME}_Error
    2.  
    3.     {PROCEDURE_BODY}
    4.  
    5.    On Error GoTo 0
    6.    Exit {PROCEDURE_TYPE}
    7.  
    8. {PROCEDURE_NAME}_Error:
    9.  
    10.     Logerror err.Number, err.Description, err.Source, False, "{PROCEDURE_NAME} of {MODULE_TYPE} {MODULE_NAME}"
    In the "Error Handler" tab of the options.

    Then add this module to your project:
    Attached Files Attached Files
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    OH, and in case anyone else is interested, you can click one button to add either just the error handler to your sub/function or you can click another button to add the error handler and a comment block to the sub/function.

    The module simply takes all the information and writes it to "error.log" in the current directory.

    OH, and when you use mztools, it will automatically fill in the procedure name and module name for you

    edit: I add this to all my projects and it saves me a TON of time.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    When I'm using classes I generally have a const for the class name
    and a const in each method with it's name and add these to the err.description
    VB Code:
    1. Err.Description = CLASS_NAME & "." & PROCEDURE_NAME & ":" Err.Description

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Here is a small demo I use for error handling.
    It records exactly which procedures the error ran through so debug is made very easy. If you force an "error" ie, "user not found" then a different style of message is displayed.

    To test both types of error message just change which error line is remarked out....

    You should ONLY EVER DISPLAY errors under click events or such like and never through custom subs/functions that u have written yourself.

    Hope this demo is useful.

    I have stripped this out of an ErrorHandling DLL I have. The full version also saves to a log file of your choice when you display the error, also it saves it to the DB....if a user has an error, then u will never have the situation where they say, I can't remember how I got it, coz you will have a 100% log of it all

    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