Using setjmp/longjmp

When setjmp and longjmp are used together, they provide a way to execute a non-local goto. They are typically used to pass execution control to error-handling or recovery code in a previously called routine without using the standard calling or return conventions.

Warning

However, because setjmp and longjmp do not support C++ object semantics, and because they might degrade performance by preventing optimization on local variables, we recommend that you do not use them in C++ programs. We recommend that you use try/catch constructs instead.

If you decide to use setjmp/longjmp in a C++ program, also include SETJMP.H or SETJMPEX.H to assure correct interaction between the functions and C++ exception handling. If you use /EH to compile, destructors for local objects are called during the stack unwind. If you use /EHs to compile, and one of your functions calls a function that uses nothrow and the function that uses nothrow calls longjmp, then the destructor unwind might not occur, depending on the optimizer.

In portable code, when a non-local goto that calls longjmp is executed, correct destruction of frame-based objects might be unreliable.

See Also

Reference

Mixing C (Structured) and C++ Exceptions