Results 1 to 11 of 11

Thread: Err.Raise Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    I have one class and one main program which calls that class. In the class, I have trap for an error and use Err.Raise method to fire the error to the main program, but it doesn't work so far.

    Any idea? Thanks

    Code:
    Public Function GetRPData() As ADODB.Recordset
            On Error GoTo errHandler
    
            'My code...bla..bla..bla...
    
            Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."
    
        End If
        Exit Function
        
    errHandler:
       'In case there is another error that is generated by system or provider
        Err.Raise Err.Number, Err.Source, Err.Description
    End Function
    
    
    '*******************Code in the main program
    Private Sub Form_Load()
       On Error GoTo errHandler
    
       'My code and declaration......
    
        Exit Sub
        
    errHandler:
        MsgBox Err.Number & "  " & Err.Description
    End Sub

  2. #2

  3. #3
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Sanon


    why do you have the line

    >>Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."

    when you have an error handler in the same function/sub????
    as soon as u have an error, if its yours or not your code will goto the handler. If you raise the error it should still goto the handler and you should not raise another error within your handler????
    Just call a msgbox.
    If its your specific error then in your handler have an
    if err.number=mynumber then
    do something
    err.clear
    end if

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    jfg: He has it that way because he wants the error to be detected in the calling funcyion and not in GetRPData. I think what he needs to do is this
    Code:
    Public Function GetRPData() As ADODB.Recordset
            On Error GoTo errHandler
    
            'My code...bla..bla..bla...
    
            Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."
    
        End If
        Exit Function
        
    errHandler:
       'In case there is another error that is generated by system or provider
        Err.Clear ' <-- NEW LINE
        Err.Raise Err.Number, Err.Source, Err.Description
    End Function

  5. #5
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    yo

    If that is the case he should remove the error handler from specific function and put it in the calling function.
    You cannot rely on vb to pass back error's from functions it's a bit hit and miss vb seems to reset errors when it leaves functions subs after there is a handler (i.e. a goto statement).

    jfg

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I'm sorry jfg but you are incorrect. It is perfectly valid to do what sanon seems to be trying to do and VB is not "hit or miss". Try this code and you will see that error 11 is reported in the calling routine.
    Code:
    Private Sub Form_Load()
    
        On Error GoTo ErrorRoutine:
        
        TheRoutineThatWillFail
        
        Exit Sub
        
    ErrorRoutine:
        If Err.Number <> 0 Then
            MsgBox "Error number " & Err.Number & " detected in calling routine"
        End If
    
    End Sub
    
    
    
    Public Sub TheRoutineThatWillFail()
    
        On Error GoTo ErrRtn
        
        Err.Raise 6
        
        Exit Sub
    
    ErrRtn:
    
        Err.Clear
        Err.Raise 11
        
    End Sub

  7. #7
    Junior Member
    Join Date
    Sep 2000
    Posts
    20
    Sorry Martin

    But I'm so use to lax programming techniques.
    Besides I'm nearly sure I had this problem with vb4 and it kept reseting errors, hold on I probibly did a

    on error goto handler

    code here

    myexit:

    exit sub

    handler:

    if err=num then
    do something
    else
    do something else
    endif

    resume myexit

    end sub

    ''''''''''''''''''''
    remember this was the recommended way to do error handlers in the olden days

    jfg

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    MartinLiss, you absolutely understand my problem correctly. I'm trying to pull out the error that is generated from my GetRPData sub which is in .dll file on the server, and then my calling Form which is a client program is on the client side. Well, if you add err.clear, the user-defined error will be gone. Therefore, this solution can't solve the problem. Moreover, I can't use msgbox as well because, as I mention above, this class is running on the server side. Thus, that message box will never been seen by the client side.

    Ok, let me explain what I'm understanding about err.raise method. As my example code, if this statement is executed, on the calling program, the on error goto errHandler should be worked immediately. Am I right? But in my program, when this situation happens, the calling program still ran to the next statement instead of going to error trap.

    What do I miss here?

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    In my example code that posted, you can assume that the TheRoutineThatWillFail rooutine is in a dll file on the server. To have it return the actual error to the client program, all you need to do is
    Code:
    Public Sub TheRoutineThatWillFail()
    
        Dim lError as Long
    
        On Error GoTo ErrRtn
        
        Err.Raise 6
        
        Exit Sub
    
    ErrRtn:
    
        lError = Err.Number
        Err.Clear
        Err.Raise lError    
    End Sub
    and error number 6 (in this case) can be handled with or without a msgbox by the client program.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    Thanks alot MartinLiss. It works just the way I want..

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You're welcome. You should also consider putting the ClassError routine below in your dll. Then call it from the error routines of each of the routines in your dll, passing the name of the routine as shown
    Code:
    Public Property Get IsAddNew() As Boolean
    
        On Error GoTo ErrorRoutine
    
        IsAddNew = m_bIsAddNew
        
        Exit Property
        
    ErrorRoutine:
    
        ClassError "IsAddNew Property Get"
        
    End Property
    
    Private Sub ClassError(sRoutine As String)
    
        Dim lError As Long
        
        lError = Err.Number
        
        Err.Clear
        'Send the error back to the main program
        Err.Raise lError, TypeName(Me) & "::" & sRoutine
    
    End Sub

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