Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Handling errors that occur in calls to object methods

Posted on 2000-04-07
4
Medium Priority
?
168 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 600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

610 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