Solved

Handling errors that occur in calls to object methods

Posted on 2000-04-07
4
162 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now