Solved

Handling errors that occur in calls to object methods

Posted on 2000-04-07
4
161 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

11 Experts available now in Live!

Get 1:1 Help Now