Solved

Handling errors that occur in calls to object methods

Posted on 2000-04-07
4
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

730 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