Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Error handling

I have a sub A which calls sub B which calls sub C ... All of these procedures have an identical error handling routine which simply ends the procedure and logs the error description.  What I would want to happen is if the error occured in sub E, I don't want the program to still execute codes in D,C,B, then down to A to where it all started.  I wanted it to go to IMMEDIATELY to the line after the call to B.  Having it execute codes in D,C,B after an error in E generated quite disasterous results.  
0
imby
Asked:
imby
1 Solution
 
schildCommented:
Well Well Well
After years of procedure design, you want to break it all in one question.
If you where a VC programmer I would just laugh at that question and that all. But lucky you, you are VB programmer since so:
The VB exception  rule is If you insert exception handler in A and you didn’t do it in B either in C, but the exception occurred in B or C it will use the A’s exception. Which means all you have to do is

Private Sub A


on error goto ContinueRunFromHere
B


Exit Sub

ContinueRunFromHere
If Err.Number = 12345 the
      Resume Next
Else
      ‘This is a real excaption, do what ever you want to do
End If

End Sub


Private Sub E


      Err.Raise 12345

But please notice you must to be careful dealing with two more issues
1. The error number you put  in “Err.Raise 12345”, be careful it will not overlap an existing one (which mostly and not always have small values in standard VB code , but on in case of  OCXs exceptions ).
2. If you are using an exception handler in B, C, D or E, you must suspend those handlers before calling “Err.Raise 12345”
you do it like this

Private Sub B

On Error Goto WhoKnowWhere ‘This B’s normal exception handler

On Error Goto 0
C
On Error Goto WhoKnowWhere ‘Bring it back for use


End Sub


Good luck
Schild



0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now