dQuiz #3
Solution
by Peter Rorlick, dBVIP

 
local x
try
  x := 1
catch( exception e )
  msgbox('A')
  return
finally
  msgbox('B')
endtry
msgbox('C')
   

When you run the code above, the user will see “A” then “B” and that’s all.

An exception occurs at the 3rd line. Why? Because the assignment operator requires that the variable or property that you are assigning to must already exist and have a value.  Even a NULL value would be acceptable.  But simply declaring local x does not give x any value.  It really just says “the variable x does not exist yet, but if it ever gets created in this function, make it local.”

So an exception is thrown at line 3, and “A” appears in a message box.

And of course, because of the return statement, the msgbox('C') won’t be executed.  But why will we see a “B” message box?  Because the FINALLY block will always get executed, no matter what happens.

Okay, I lied.  If the CATCH block includes a quit statement, the FINALLY block won’t get executed.

For a more real-world-ish example of the usefulness of TRY… CATCH… FINALLY, have a look at the examples in the dB2K On-Line-Help.


Back to the dQuiz