|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Clear StackHi all,
Is there a way to clear the event queue and the stack? I have a communication program where I am several sub routines deep and in the event of an error on the other end, I need to keep the program from returning to all the subs. I could write a ton of code to do this, but I really would like to simply clear the queue. Bill On 18 Oct 2005 01:21:31 -0700, theycallmeb***@gmail.com wrote:
>Hi all, I think that you'll find that On Error does just that> >Is there a way to clear the event queue and the stack? > >I have a communication program where I am several sub routines deep and >in the event of an error on the other end, I need to keep the program >from returning to all the subs. I could write a ton of code to do this, >but I really would like to simply clear the queue. On Error Resume Next Call SomeDodgyStuff If Err Then ' we land up here Try this :- Option Explicit Private Sub Command1_Click() On Error Resume Next Call SomeDodgyStuff If Err Then MsgBox "Some Error" End Sub Private Sub SomeDodgyStuff() Dim A$, B$, C$ Me.Print "Enter SomeDodgyStuff" Call SomeDodgyStuffA Me.Print "Leave SomeDodgyStuff" End Sub Private Sub SomeDodgyStuffA() Dim A$, B$, C$ Me.Print "Enter SomeDodgyStuffA" 'Error 99 ' Uncomment this Me.Print "Leave SomeDodgyStuffA" End Sub As you can see, the call stack is unwound without execution I strongly suggest that you implement error handling at lower levels then turn it off and 'manually' raise an error if you want to jump straight back out. Also use Err.Clear on a good exit (which might not be as 'good') eg: Private Sub SomeDodgyStuff() Dim A$, B$, C$ Me.Print "Enter SomeDodgyStuff" On Error Resume Next DoSomething If Err = 123 Then ' Known Error On Error GoTo 0 Error 99 ' Skip Out End If If Err Then MsgBox "Unexpected Error" Err.Clear End If ' Good Exit Me.Print "Leave SomeDodgyStuff" End Sub Personally I'm not to happy with this Throw and Catch stuff as it can lead to 'unexpected' errors getting disguized - but sometimes it is necessary ... To unwind the stack, just raise an exception that's handled by a procedure
near the root of your stack. Reserve a dedicated error code for this purpose, e.g. Public Const ERR_UNWIND As Long = vbObjectError OR 1000 When necessary, raise this using something like: Err.Raise ERR_UNWIND, TypeName(Me), "Unwinding stack" In all your local error handlers, test for this explicit error code. If that's the one then re-throw the error, else handle it as per normal, e.g. LocalErrHandler: If Err.Number = ERR_UNWIND Then ' Re-raise the error for someone else to handle Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext Else ...handle the error as per normal... End If That reserved error code will eventually find its way to your root procedure, at which point your stack will have been reset as requested Tony Proctor <theycallmeb***@gmail.com> wrote in message Show quoteHide quote news:1129623691.318589.155340@g14g2000cwa.googlegroups.com... > Hi all, > > Is there a way to clear the event queue and the stack? > > I have a communication program where I am several sub routines deep and > in the event of an error on the other end, I need to keep the program > from returning to all the subs. I could write a ton of code to do this, > but I really would like to simply clear the queue. > > Bill > |
|||||||||||||||||||||||