Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling - With function running multiple functions 1

Status
Not open for further replies.

saraUSIT

Programmer
Jul 15, 2009
20
US
How would I get a main function to exit all successive functions if there is an error in one of them. I created an error handler on the main function, but I needed to create an additional error handler in one of the functions - because if there is an error in that specific function somewhere in the middle of running I need it to revert the changes it made. Upon testing it - the local error handler worked ok and exited that function but then it resumed running the rest of the functions from the main function and it did not go to the error handler on the main function which causes problems to the next function.
 
You can have the called function return a specific value if it does not pass. Then the main function can determine if the called function passed or not. If it did not pass without failure then exit the main.
 
MajP,
Thanks for your help, but I'm not sure how to code it - this function actually has a parameter that gets passed into it from the main function. How would I get the function to return a different value when it errors out to the main function? I tried adding code into the error handler with the function name equalling some value - but the program wouldn't run.
 
Here is a simple example

Code:
Public Function mainFunction(x As Double, y As Double) As Double
   Dim varRatio As Variant
   x = x + 1
   y = y - 1
   varRatio = getRatio(x, y)
   If IsNull(varRatio) Then
     MsgBox "Failure in called routine exiting main function"
     Exit Function
   End If
   mainFunction = varRatio * 3
End Function

Public Function getRatio(x As Double, y As Double) As Variant
  On Error GoTo errlabel
  getRatio = x / y
  Exit Function
errlabel:
  MsgBox Err.Number & "  " & Err.Description
  getRatio = Null
End Function

the main function calls getRatio. If getRatio throws an error the error check sets the return value to null. The main checks to see if the return value is null.

I think the other way to do this is to trap the error, do your fixes, and then raise the error inside of your trap. This passes the error to the main function

Public Function calledFunction() As Variant
On Error GoTo errlabel
calledFunction = 3 / 0
Exit Function
errlabel:
MsgBox Err.Number & " " & Err.Description
'do some code to reset values
'No raise the error passing control to main function
Err.Raise (Err.Number)
End Function
 
Thanks MajP,
I used the raise error function and it worked beautifully. Thanks for the help! I was just wondering about a line of the code you provided (I know it has nothing to do with my original question - but I always like to learn new things...)
You put into the sample code the following line:
Code:
calledFunction = 3 / 0
Would you mind to explain what that would do? Thanks
 
I was just forcing the function to error out, by setting the value of the function to 3 divided by 0. This gives a division by zero error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top