67
Select Case SetJmp(jmpBuf1)
Case 0
' control came back from SetJmp()
debug.print "calling foo()"
Call foo()
debug.print "normal return from foo()"
Case 1
' control came back from LongJmp()
debug.print "SetJmp() returned 1 via LongJmp()"
End Select
debug.print "end test"
End Sub
Sub foo()
debug.print "in foo()"
Call bar()
debug.print "returning from foo()"
End Sub
Sub bar()
debug.print "in bar()"
Call LongJmp(jmpBuf1, 1)
debug.print "returning from bar()"
End Sub
Running this program will result in the following debug output:
start test
calling foo()
in foo()
in bar()
SetJmp() returned 1 via LongJmp()
end test
Note that the last lines of neither foo() nor bar() were executed nor was the line after the call to
foo() in Main(). If you comment out the call to LongJmp() in bar(), the program will produce the
following debug output:
start test
calling foo()
in foo()
in bar()
returning from bar()
returning from foo()
normal return from foo()
end test
Note that a call to LongJmp() generally should not utilize a value of zero as the second parameter.
Doing so will make it appear as though the original SetJmp() call is returning.
The jump buffer may also be a local variable if desired. The example code below is a modified version of
the previous example showing how the jump buffer is passed down the hierarchy as a parameter. This
technique may be used to create generalized subroutines that might return to one of several places
depending on how it was called.
Sub Main()
Dim jmpBuf1(1 to System.JumpBufSize) as Byte
debug.print "start test"