Im having some issues with Error handling or maybe the understanding thereoff. Say I have a function TopFunction which calls a function MiddleFunction 10 times which again calls a function LowFunction 10 times.
Now If LowFunction throws a fatal error and I have no error handling, access is "reset" so to speak. All variable values are wiped from memory, and code execution is stopped (or possibly paused with debug options if its a development version).
However if I do have error handling and catch a unexpected error (One I did not anticipate and therefore basicly don't know how to handle) I want to quit the LowFunction, BUT I also want to quit MiddleFunction and TopFunction from proceeding. Simply exiting the LowFunction just makes middlefunction proceed with the next function call of LowFunction.
Not exactly sure these are the right words to describe it, but its the best I can do. How should I propagate the error back up through MiddleFunction and TopFunction?
OR If you feel im barking up the wrong tree, please explain how you guys handle this. 5 4983 NeoPa 32,568
Recognized Expert Moderator MVP
I suspect when you say function, you really mean procedure. The trick is to ensure the procedure is a function procedure, and have a return value that indicates an error.
When a lower-level function returns a value indicating an error has occurred then you have a choice of returning an error indicator straightaway, or throwing an exception (Raise) yourself to trigger the error-handling at the current level (which itself, would presumably handle returning an error value as well as anything else necessary to handle errors for that procedure).
Does that help?
Thank you for your reply.
Yea, I was thinking along those lines, but was wondering if there was something more efficient.
My concern is that it means (As I understand it) that after each run of LowFunction I would need to have an IF statement to check whether or not it threw an error, adding extra overhead to my code. If that is the only way to go about it, then so be it, I was just hoping there might be a more efficient setup. I do realise that in alot of cases its a very very slight overhead, but still. I want to do it the Right/Best Practice way, and being self-taught I sometimes wonder if I am doing that.
Another thing I noticed is that if MiddleFunction has an errorhandler, but LowFunction does not, then the error is handled (or attempted to be handled) in MiddleFunction. I was wondering if I could use that somehow. The issue with this approach is that LowFunction might be reused in many places, and I might desire it to have error handling some times, and not other times (in which I want the higher level function to handle it).
P.s.
Its not necessary to have it be a function. You could do it with a sub as well (Example below) - Private sub MiddleFunction()
-
Dim errNo as long
-
LowFunction errNo
-
If ErrNo then
-
Err.Raise ErrNo
-
End if
-
Exit sub
-
End Sub
-
-
-
Private Sub LowFunction(errNo as long)
-
On Error goto ErrHandler
-
'Code here
-
-
-
exit Sub
-
ErrHandler:
-
errNo=Err.Nr
-
Exit Sub
-
End Sub
NeoPa 32,568
Recognized Expert Moderator MVP
It is possible to use a Sub procedure as you say. I would certainly consider declaring it explicitly as ByRef in that case though.
An alternative I hinted at, when it is required for the higher level function to handle the error instead, would be to Raise the same error after ensuring error handling had been disabled. A higher level error handler will always pick up errors in lower levels when they occur without any active error handler therein.
Otherwise, error handling does appear to be a little clumsy in VBA. Not as sophisticated as many parts of the syntax for sure.
ADezii 8,834
Recognized Expert Expert @TheSmileyCoder :
Sorry about coming in a little late, but I found your scenario very interesting, and a challenge to resolve. Give a series of Nested Functions(3), calling each other multiple times, you can actually control as to whether the Top Level or Bottom Level Function controls Error Handling. - Enable Error Handling in the Top Level Function:
- Public Function TopFunction()
-
On Error GoTo Err_TopFunction
-
-
Dim bytCtr As Byte
-
-
'Call Middle Function 3 Times
-
For bytCtr = 1 To 3
-
Debug.Print "Top: " & Format$(bytCtr, "00")
-
MiddleFunction
-
Next
-
-
Exit_TopFunction:
-
Exit Function
-
-
Err_TopFunction:
-
MsgBox Err.Description, vbExclamation, "ERROR Somewhere"
-
Resume Exit_TopFunction
-
End Function
- Do not enable Error Handling in the Middle Level Function:
- Public Function MiddleFunction()
-
Dim bytCtr2 As Byte
-
-
'Call Low Function 3 Times
-
For bytCtr2 = 1 To 3
-
Debug.Print " |-- Middle: " & Format$(bytCtr2, "00")
-
LowFunction
-
Next
-
End Function
- Enable Error Handling in the Low Level Function, in conjunction with the #If...Then...#E lse Directive:
- Public Function LowFunction()
-
#If HANDLE_LOW_ERRORS Then
-
On Error GoTo Err_LowFunction
-
#End If
-
-
Dim bytCtr3 As Byte
-
-
'Call Low Function 3 Times
-
For bytCtr3 = 1 To 3
-
If bytCtr3 = 3 Then Err.Raise 13
-
Debug.Print " |-- Low: " & Format$(bytCtr3, "00")
-
Next
-
-
Exit_LowFunction:
-
Exit Function
-
-
Err_LowFunction:
-
MsgBox Err.Description, vbExclamation, "Error in LowFunction()"
-
Resume Exit_LowFunction
-
End Function
- Declare a Conditional Compilation Constant that will control which Level Function (Top/Bottom) will handle Errors:
-
'Top Level Function will handle Errors
-
#Const HANDLE_LOW_ERRORS = False
-
-
'Bottom Level Function will handle Errors
-
'#Const HANDLE_LOW_ERRORS = True
-
- With HANDLE_LOW_ERRO RS = False, the Error will travel up the Stack, and be handled by the Top Level Handler. A Single Error Message will be generated.
- With HANDLE_LOW_ERRO RS = True, the Error will be handled by the Bottom Level Handler. Multiple Error Messages will be generated.
- Debug.Print Statements are for Testing Purposes only.
- Code has been tested, and appears to be fully operational.
NeoPa 32,568
Recognized Expert Moderator MVP
That's a good illustration of what the Context-Sensitive Help system has to say on the matter.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Lian Liming |
last post by:
Hi, all
I want to write my own error handler function in php by using
"set_error_handler()" function. I have made a test for this function but
found it did not work. My test code is as following:
<?php
function myerrorHandler ($errno, $errstr, $errfile, $errline,
$errcontext)
{
switch ($errno)
{
|
by: evan.cooch |
last post by:
Greetings.
Suppose I have some function called "CheckIt" - some function to
validate form data before submitting it to e CGI script. Pretend the
name of the form is "TheForm".
If I use the following code, everything work perfctly:
<Input type=button VALUE="Submit your form"
onClick="javascript:return CheckIt(TheForm)">
|
by: deanbrown3d |
last post by:
Hi there!
Suppose I have a function
void ShowMessage(string s);
(that has a void return type.)
In my other function F, also with a void return type, can I do this?
|
by: murphy |
last post by:
Hi,
I've been seeing two symptoms with my asp.net site that have started
recently after a long period of smooth running. As others on our team
make changes to referenced dll's I find that I get the following
errors from time to time.
Apparently the following procedure alleviates the problems:
|
by: kathy |
last post by:
how to return array from function?
| |
by: Alexander Cohen |
last post by:
(sorry for the double post if there is one - i sent the mail to the
lisyt from the wrong address)
Hi,
Im passing this in the commmand line to start up the PostgreSQL server:
../pg_ctl start -w -D /Volumes/GROUCH\ 2/Database3
but its always giving me this error:
|
by: Jozef |
last post by:
I have an MDE file that is blowing up with "Error 3075 Function Not
Available", which is normally a reference issue, but, I have no missing
references.
It seems that offending function is a Format() over a date field in a list
box. Has anyone run into anything similar before? I built the db on a
Windows XP pro machine using Access XP. The target / test machine is a
Windows 2000 machined with only Access 97 and the runtime version of...
|
by: msolem |
last post by:
I have some code where there are a set of functions that return
pointers to each other. I'm having a bit of a hard time figuring out
the correct type to use to do that.
The code below works but I'm defining the functions as void*, and then
casting when I use them. This code is going into a general purpose
framework, and it would be much nicer if the user didn't need to do
any casting.
Can someone tell me how to set up those typedefs...
|
by: Sonasang |
last post by:
Hi ,
I am creating a web page with ASP and Javascript.We have shared the foldres containg the code and all our team members are accessing the code.
There is no problem for me when i run the code and working properly. But when some of my team members when they run the web page they are getting the ERROR in function() Operation is not allowed when the object is closed.
In the login page there in no problem, After we login in the page...
|
by: kinhoe |
last post by:
i have a program in webserver. i run smooth before. but dunno why it sudden prompt out the error below?
Warning: require_once() : open_basedir restriction in effect. File(/var/www/vhosts/db2020.co.uk/application/application/config/admin/config.live.php) is not within the allowed path(s): (/var/www/vhosts/db2020.co.uk/subdomains/admin/httpdocs:/tmp) in /var/www/vhosts/db2020.co.uk/subdomains/admin/httpdocs/index.php on line 11
Warning:...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |