Results 1 to 5 of 5

Thread: [Resolved] Error handling in class module

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    52

    Resolved [Resolved] Error handling in class module

    Friends,

    Following is a method in one of my VB6.0 class that I intend to deploy as COM+ application. I have a phobia handling errors... I guess. Please comment on the simple error handling I have used. And any suggestions/advise is highly appreciated.
    Thanks in advance!

    "RaiseError" is a sub that simply does
    VB Code:
    1. err.raise eNo, eSrc, eDesc


    VB Code:
    1. Public Function GetCustomClients(ByVal bRegClient As Boolean) As ADODB.Recordset
    2.  
    3.   '==================================================
    4.   '  Parameters In: Boolean flag; True for regular clients
    5.   '  Returns      : Recordset containing - Client-Id, Client-Name
    6.   '                 and Description
    7.   '  Description  : Used to build the User-Interface (list-box)
    8.   '==================================================
    9.  
    10.   On Error GoTo errHandler
    11.  
    12.   Dim cClient As New ADODB.Command
    13.   Dim pFlg As ADODB.Parameter
    14.  
    15.   eNo = 0                   'declared form-level
    16.   eDesc = vbNullString                                   'form-level
    17.   eSrc = "GetCustomClients"                           'form-level
    18.  
    19.   cClient.ActiveConnection = ConnStr
    20.   cClient.CommandType = adCmdStoredProc
    21.   cClient.CommandText = "qryCustomClients"
    22.  
    23.   Set pFlg = cClient.CreateParameter("bFlg", adBoolean, adParamInput, , bRegClient)
    24.   cClient.Parameters.Append pFlg
    25.  
    26.   Set GetCustomClients = cClient.Execute()
    27.  
    28. errExit:
    29.   Exit Function
    30.  
    31. errHandler:
    32.   If Err.Number > 0 Then
    33.     eNo = Err.Number
    34.     eDesc = Err.Description
    35.     Err.Clear
    36.     On Error GoTo 0
    37.     Call RaiseError
    38.   End If
    39. End Function
    Last edited by vbXML; Jun 21st, 2005 at 07:55 AM.

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Error handling in class module

    I personally would do something like this:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function GetCustomClients(ByVal bRegClient As Boolean) As ADODB.Recordset
    4. 10        On Error GoTo errHandler
    5.           Dim cClient As New ADODB.Command, pFlg As ADODB.Parameter
    6. 20        eNo = 0:  eDesc = vbNullString:  eSrc = "GetCustomClients"
    7. 30        cClient.ActiveConnection = ConnStr
    8. 40        cClient.CommandType = adCmdStoredProc
    9. 50        cClient.CommandText = "qryCustomClients"
    10. 60        Set pFlg = cClient.CreateParameter("bFlg", adBoolean, adParamInput, , bRegClient)
    11. 70        cClient.Parameters.Append pFlg
    12. 80        Set GetCustomClients = cClient.Execute()
    13.          
    14. 90        Exit Function
    15.          
    16. errHandler:
    17. 100       HandleError Err
    18. End Function
    19.  
    20. Public Sub HandleError(ByRef Err As ErrObject)
    21.     Dim strErrorPrompt As String
    22.     strErrorPrompt = "Error Number: %num%" & vbCrLf & "Error Desc: %desc%" & vbCrLf & "On Line: %erl%"
    23.     strErrorPrompt = Replace(strErrorPrompt, "%num%", Err.Number)
    24.     strErrorPrompt = Replace(strErrorPrompt, "%desc%", Err.Description)
    25.     strErrorPrompt = Replace(strErrorPrompt, "%erl%", Erl)
    26.    
    27.     LogToFile (Err.Number & vbCrLf & Err.Description & vbCrLf & Erl)
    28.     ''
    29.     '' or
    30.     ''
    31.     LogToEventViewer (Err.Number & vbCrLf & Err.Description & vbCrLf & Erl)
    32.     ''
    33.     '' or
    34.     ''
    35.     LogToDB (Err.Number & vbCrLf & Err.Description & vbCrLf & Erl)
    36. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    52

    Re: Error handling in class module

    Thanks much, plenderj! I heartly appreciate it.

    since it is a class I wanted to raise error to the calling application, is that okay?

    I like your idea of having a central method like "HandleError". Would it be a good idea to make it dll. May be add some error constants to it too.
    (But somewhere on this forum I read that a general method to handle error isn't advisible).

    Thanks!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Error handling in class module

    Quote Originally Posted by vbXML
    Thanks much, plenderj! I heartly appreciate it.

    since it is a class I wanted to raise error to the calling application, is that okay?
    Yes.
    Quote Originally Posted by vbXML
    I like your idea of having a central method like "HandleError". Would it be a good idea to make it dll. May be add some error constants to it too.
    If you are going to use this in multiple applications, then again, I would say 'yes'.
    Quote Originally Posted by vbXML
    (But somewhere on this forum I read that a general method to handle error isn't advisible).
    A general method is just that, a GENERAL method. It would handle your garden variety type of errors, but such a practice is discouraged because you still wind up having to deal with errors that are specific to the way your code is written. Unless you want to spend the time writting a .Dll that will cover every single possible error that could ever occur in an application, you will always need specific error trapping written in.

    Of course, your .Dll could just be general. In other words, regardless of what the error is, you could just write it off to something and have the program continue. This would ensure that your program wouldn't bomb on a runtime error, but it probably wouldn't be very useful in attempting to determine what happened and why it happened.
    Last edited by Hack; Jun 21st, 2005 at 07:57 AM.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    52

    Re: Error handling in class module

    Thank you much for the detailed explaination, Hack! Thats huge!

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