Is this done? (Err.Raise Err)

hbrems

has no clue...
Local time
Tomorrow, 00:18
Joined
Nov 2, 2006
Messages
181
Hi,

I'm making a simple Access application where I have a few classes with a couple of methods in each of them. When an error occurs in one of these methods I'd like to throw it to the 'parent' (originator) of the function.

In c# you would have something like this:

Code:
try
{
       #METHOD EXECUTION#
}
catch (exception ex)
{
       Throw ex;
}

I found out that I can do something similar in VBA in this form

Code:
On error goto ErrorHandler
       #METHOD EXECUTION#
ErrorHandler:
       Err.Raise Err

Would this be a valid way of throwing the error in VBA?
 
Last edited:
Unfortunately, VBA doesn't provide catch and throw methods for handling errors and exceptions. As you've figured out, you can raise a user-defined error and handle it in the error handler (within the same function/sub), but don't try to send it to a handling function because that may just lead to a loop and you will find it hard to trace errors.

These links should provide you with enough info re error handling:

http://www.cpearson.com/excel/errorhandling.htm
http://www.fmsinc.com/tpapers/vbacode/debug.asp
http://www.developerfusion.com/article/1741/handling-errors-in-vbvbavbsasp/7/
http://msdn.microsoft.com/en-us/library/aa164019%28v=office.10%29.aspx#odc_tentipsvba_topic3

One of the links explains how you can have a "global" error handler.
 
From Access help…

Break on Unhandled Errors — If an error handler is active, the error is trapped without entering break mode. If there is no active error handler, the error causes the project to enter break mode. An unhandled error in a class module, however, causes the project to enter break mode on the line of code that invoked the offending procedure of the class.

You will see a lot of class module code without error handling particularly with property Get Set code. That type of code will raise the error in the caller not within the class. Kind of makes me wonder why people still say that all procedures should have error handling when, in fact, there are times it shouldn’t.

Maybe all you need to do is set error handling to 'Break on Unhandled Errors'.

Chris.
 
I usually rethrow error generally in class modules where I intended it to be consumed by different projects. Because it's typical that a VBA project for Access is only for the same project and won't be shared out, there's not much benefits in adopting common patterns that we see in other languages. But back in class modules, there's usually a benefit since if I simply passed back the error unaltered, it may not be all that clear why the error is occurring and from which piece of class module it is coming from. Re-throwing allows me to alter various properties to provide useful diagnosis information.
 
Hi Chris,

not sure if I understand you correctly but I was more referring to supplying the Err object as parameter for the Err.Raise method instead of inquiring about the Err.Raise method in general.

I feel that maybe a class (or function) should not communicate the exception to the end user. Instead when an exception occurs I'd like my class to communicate it to the requester (fe. form or other class) and take it from there.

Take this example:

Code:
' FORM CODE
Public Sub btn_Click()
     on error goto ErrorHandler
          SomeMethod
     Exit sub

     ErrorHandler:
          MsgBox "Error: " + Err.Description
Exit Sub

' METHOD CODE
Public Function SomeMethod()
     on error goto ErrorHandler
          ' Cause error
          SomeMethod = 1 / 0
     exit Function

     ErrorHandler:
          Err.Raise Err
End Function

So when something goes wrong in the method, the exception is sent back to the form which in turn will communicate to the end user what went wrong. This is meant to be a simplistic general error handling strategy for a small project with a limited time schedule.

I'm trying to 'simulate' a 'throw' in c#
 
Set error handling to 'Break on Unhandled Errors'.

Behind Form1: -
Code:
' METHOD CODE IN SOME CLASS MODULE
Public Function SomeMethod()
          
    ' Cause error
    SomeMethod = 1 / 0

End Function


Run this code from behind Form2 (Form1 needs to be open): -
Code:
' FORM CODE
Public Sub btn_Click()

     On Error GoTo ErrorHandler
     
     Forms("Form1").SomeMethod
     
     Exit Sub

ErrorHandler:
    MsgBox "Error: " + Err.Description

Exit Sub

End Sub

The error is handled in the caller.

Chris.
 

Users who are viewing this thread

Back
Top Bottom