BUG: Goto() Within Try-Except Block Causes C2705 Error (122541)



The information in this article applies to:

  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.2
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q122541

SYMPTOMS

A Goto statement within a try-except block causes the following incorrect error if the destination of the Goto is within the same try-except block:
error C2705: '<label>' : illegal jump into __try scope

RESOLUTION

One workaround demonstrated in the example in the More Information section of this article is to remove the extra braces around the block containing the Goto. This places the goto() at the same nesting level as the destination.

Another workaround is to use the __leave keyword to jump to the end of the try-except block. If you do this, execution resumes after the end of the exception handler. One difference between __leave and Goto is that the __leave statement does not cause stack unwinding, so it is more efficient than goto().

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

The following sample code can be used to demonstrate both the problem and the workaround:

Sample Code

/* Compile options needed: none
*/ 
class TestClass {
      public:
        TestClass() {}
       ~TestClass() {}
};
void main (void) {
    __try
    {
       {
       TestClass test;
       if (1) {
          goto bail;
//         __leave;    // Use this instead of the goto
       }
    }
    bail:  ;
    }
    __except(0) { }
}
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbbug kbcode kbCompiler KB122541