Search | Synergex.com | Contact Us | Resource Center Login | Home|
News

What is the output of the following contrived program?

main
record
    a          ,int
    x          ,int
proc
    open(1,o,'TT:')
    goto label
    try
      begin
        a = 1
label,
                x = 1 / a
                writes(1,"fine")
      end
    catch (e, @Exception)
      begin
        writes(1,"error")
      end
    endtry
end

a. "fine"
b. "error"
c. A compile error
d. A runtime error

Explanation:
When we reach the line “x = 1 / a”, we will print "fine" (a) if no error occurs. That would be the case if the variable a is non-zero. However, the GOTO statement above attempts to branch around the assignment to a, and in Synergy/DE, a numeric field without an initial value will be zero as long as it is in a local or global record (not stack or member variables). Thus it appears that (b) would be the answer.

However, the GOTO is from outside the TRY block, so you might think that the trapping mechanism is not in effect (d). That could lead to some very confusing behaviors. It turns out that the compiler disallows this (c). In fact, any branch into or out of a block that introduces a new scope is not allowed. In this case, the scope is created by the TRY — but you would also see a compilation error when branching into or out of a block that defines object handles with the DATA statement, for instance, because their scope is bounded by the block.

Back in the old days, branching into or out of a BEGIN/END block was sometimes the best way to handle certain conditions, due to limitations of the older DIBOL syntax. Therefore, it's still supported within limits. For instance, if the GOTO were placed inside the TRY block, it would work as expected.

Nowadays, though, Synergy/DE provides many better alternatives to using GOTOs.  We all know that GOTOs are considered harmful, so while you're adding that nicely structured TRY/CATCH, lose the labels.

More information about News and Events

Contact Synergex to learn more about  News and Events