Solved

Handling errors that occur in calls to object methods

Posted on 2000-04-07
4
163 Views
Last Modified: 2010-05-02
I have a Form that makes a call to a method of an object variable.  An error occurs in that method and I want to handle it in the form.  If the call was to one of the form's own methods everything would work as I would want.  As it is my error handler is never called and execution is halted.  I can't imagine that this is there is no simple soution to this, but I can't find it.  The smple code below illustrates the problem:

A Form that makes a method call on an object:

Private Sub Form_Load()
  Dim T As New Test  'The object - see below
 
  On Error GoTo HandleError
  T.Foo 0
  Exit Sub

HandleError:
  Debug.Print "Handling error in Foo call at top level" 'I never get here!
End Sub


The object code:

Public Sub Foo(Divisor As Integer)
  Dim X As Integer

  X = 1 / Divisor  'Division by zero error occurs.
End Sub

0
Comment
Question by:jleinbach
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2694844
why dont you check for the value of the divisor before performing the calculation?

If Divisor = 0 Then MsgBox "Can't divide by 0!"
Else
   x = 1/Divisor
0
 
LVL 1

Expert Comment

by:ndb
ID: 2694852
Why don't you want errorhandling in that subroutine?
It's a good programming technique to do so in every method.
You can try the following:

******************************

Private Sub Form_Load()
  Dim T As New Test  'The object - see below
   
  On Error GoTo HandleError
  T.Foo 0
  Exit Sub

HandleError:
  Debug.Print "Handling error in Foo call at top level" 'I never get here!
End Sub


The object code:

Public Sub Foo(Divisor As Integer)
  On Error GoTo Hell
  Dim X As Integer

  X = 1 / Divisor  'Division by zero error occurs.
  Exit Sub

Hell:
  Err.Raise Err.Number,Err.Source,Err.Description
End Sub



******************
0
 
LVL 2

Accepted Solution

by:
SamratAshok earned 150 total points
ID: 2695046

I am making an assumption that you are using VB6. Make necessary
adjustments if this is not the case

In VB IDE
click on Menu->Tools->Options
Click on General Tab and set
Error Trapping at "Break on Unhandled Errors"




0
 

Author Comment

by:jleinbach
ID: 2695246
Thank you!  This is exactly what I was looking for.  I couldn't believe that such as thing was not a standard feature of VB.  It is... you just have the option to turn it off.  :)

Responses to the comment posters follow(for what it's worth):

AzraSound:

I'm not actually trying to avoid a divide by zero error.  That was just sample code.  In general one does need to be able to catch errors raised at one level in the stack at some other, higher, level.  In fact, that is pretty much the purpose of exception handling schemes.

ndb:

Good suggestion.  And I am in fact doing exactly that, with the reraising just as you did it (I took it out of the sample just to make it simpler).  Unfortunately it still halted right at that second raise.  This was of course do to the error handling setting being 'Break in Class Module' as opposed to 'Break on Unhandled Errors' as SamratAshok so succinctly pointed out.

I'm all set now.  Thanks to everyone for the help.

Jared
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question