C has its own way of handling exceptions through the utilization of setjmp and longjmp. The setjmp function marks a place in a program.The longjmp function jumps to the place marked by setjmp. Normally setjmp returns a zero. This tells the program to execute normal code.

When an exception occurs, the longjmp call returns to the situation of the setjmp function. The only difference the program can see between a real setjmp call and a fake setjmp call caused by a longjmp is that a normally setjmp returns a zero.

When setjmp is “called” by longjmp, the return value is controlled by a parameter to longjmp.The definition of the setjmp function is:

#include <setjmp.h>

int setjmp(jmp_buf env);



is the place where setjmp saves the current environment for later use by longjmp.

0 Normal call Nonzero
Non-zero return codes are the result of a longjmp call.

The definition of the longjmp call is:

void longjmp(jmp_buf env, int return_code);

where: env

is the environment initialized by a previous setjmp call


is the return code that will be returned by the setjmp call.There is one problem here, however. The longjmp call returns control to the corresponding setjmp.

It doesn’t call the destructors of any classes that are “destroyed” within the process.we can see that in the subroutine we define a class named a_list.Normally we would call the destructor for a_list at the end of the function or at a return statement. However, during this case we use longjmp to exit the function.

Since longjmp is a C function it knows nothing about classes and destructors and does not call the destructor for a_list.

So we now have a situation where a variable has disappeared but the destructor has
not been called. The technical name for this example may be a “foul-up.”When converting C to C++, change all setjmp/longjmp combinations into exceptions.

setjmp and longjmp example:-



jmp_buf location; // place to storeloaction data

void subroutine(void)
class list a_list 

//here we detect an exception
// handle it by "longjmp" out of the routine


if(setjmp(env)== 0){

cout<<"normal execution*n*";





If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Uses setjmp and longjmp in C++

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories


Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page


Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
    Strength indicator
    Log In | Lost Password