views:

54

answers:

2

I have two procedures procA and procB. procA is calling procB. An exception occurs within procB. I can handle the exception within procB, but i like to handle it within procA and this is what i did not get to work. I'm not very familiar with VB6 but i think this should be possible because MSDN says:

If an error occurs while an error handler is active (between the occurrence of the error and a Resume, Exit Sub, Exit Function, or Exit Property statement), the current procedure's error handler can't handle the error. Control returns to the calling procedure. If the calling procedure has an enabled error handler, it is activated to handle the error.

What i'm doing wrong?

Now the code fragments:

Private Sub procA()
  On Error GoTo ErrHnd
  ...
  procB obj
  Exit Sub
ErrHnd:
  MsgBox Err.Description, vbInformation, Me.caption
End Sub

Public Sub procB(ByRef rec As Object)
  On Error GoTo ErrHnd
  ... Exception occurs within DAO Recordset Operation
  Exit Sub
ErrHnd:
  Select Case Err.Number
  Case 3022
    Err.Raise vbObjectError + 9999, Err.Source, "Error Text"
  Case Else
    ...
  End Select
End Sub

I also tried to turn off exception handling within procB (On Error Goto 0) but it seems that procA never gets the Exception.

Thanks for your help.

Edit: Additional information:

  • Exception Raised from DAO.Recordset Object.
  • I also tried to completele remove exception handling within procB with no effect.
  • procA exists in another file then procB (data.cls, frmListArtikel.frm).

Solution: I didn't know that it makes a difference how the programm is executed. If i start it from the IDE, the Exception does not get handled by procA. If i start the EXE (previously making it from the IDE) from the Explorer, the Exception gets handled as desired by procA.

+4  A: 

You can only have one active error handler at a time. If you activate in procb, procb will handle.

you may also need to check your editor settings. choose the option "Tools > Options > General tab" "break in class module"

Code sample 1. you will receive error 6 in procA:

Private Sub Form_Load()
    Call procA
End Sub

Private Sub procA()
    On Error GoTo errhan
    procB
    Exit Sub
errhan:
    Debug.Print "proca handle"
End Sub

Private Sub procB()
    Err.Raise 6
End Sub

Code sample 2. You will receive error 7 in procA:

Private Sub Form_Load()
    Call procA
End Sub

Private Sub procA()
    On Error GoTo errhan
    procB
    Exit Sub
errhan:
    Debug.Print "proca handle"
End Sub

Private Sub procB()
    On Error GoTo errhan
    Err.Raise 6
errhan:
    Err.Raise 7
End Sub
bugtussle
+1 This is probably the solution; I spent hours on a similar problem before I realized that this option wasn't set.
derekerdmann
+1 I would also suggest in the original code posted in the question, using `On Error Goto 0` before the `Err.Raise `. That does mean you need to cache `Err.Source` in a variable because `On Error Goto 0` clears the `Err` object.
MarkJ
Thank you for the answer, i deleted 'On Error Goto ErrHnd' in procB and also checked 'break in class module'. Unfortunately procA still doesn't get the Exception. In my special case it must be something else. Do exceptions go accross module boundaries (file1.frm, file2.frm)?
Christian Ammer
Yes exceptions go across forms and modules. You should try my example in an empty project and verify that it works.
bugtussle
A: 

From my experience of exception (error) handling in VB6 - It is rubbish. Very temperamental.

I don't think I've ever seen anyone successfully 'throw' a VB6 error up to the calling procedure.

My advice would be to not even try. Instead have ProcB return a status variable of your own devising. eg.

Private Sub procA()
  On Error GoTo ErrHnd
  ...
  statusProcB = procB(obj)
  If statusProcB<>0 then
     MsgBox Err.Description, vbInformation, Me.caption 
  Endif
  Exit Sub
End Sub

Public Function procB(ByRef rec As Object) As Long
  On Error GoTo ErrHnd
  ... Exception occurs within DAO Recordset Operation
  ProcB=0
  Exit Sub
ErrHnd:
  Select Case Err.Number
  Case 3022
    procB=1
  Case Else
    ...
  End Select
End Sub
El Ronnoco
No, VB6 exception handling works fine in my experience. We "throw" VB6 errors up to the calling procedure all the time. Inside the same project, across component boundaries (ActiveX EXE, ActiveX DLL). It works fine.
MarkJ
Apologies. I stand corrected! I must just have never figured out how to use it properly!
El Ronnoco