Question

I'm trying to use setjmp/longjmp for error handling, however, the call to longjmp is causing the programme to exit with code 0 when compiled using MSVC 2010, here is the full message:

The program '[5020] test.exe: Native' has exited with code 0 (0x0).

here is the code:

#include <setjmp.h>
#include <stdio.h>

int main(void)
{
    jmp_buf env;
    switch(setjmp(env))
    {
    case 0:
        printf("normal operation\n");
        longjmp(env, -2);
        break;
    case -1:
        printf("known error\n");
        break;
    default:
        printf("Unknown error!\n");
        break;
    }
    return 0;
}

I've compiled the same code using a gnu based compiler (bfin-elf-gcc under cygwin) which worked fine. I.e.,

$ make
bfin-elf-gcc -c -Wall main.c -mcpu=bf533-any -o main.o
bfin-elf-gcc main.o -mcpu=bf533-any -msim -o bfin_test

$ bfin-elf-run.exe bfin_test
normal operation
Unknown error!

Any idea why it is not working on msvc?

Many thanks in advance, Hasan.

Was it helpful?

Solution

longjmp(env, -2); triggers your default: case which prints Unknown error! and then emerges from your switch statement, where return 0; is performed. It's working exactly as you wrote it. Returning 0 from main() is essentially the same as exiting with 0. MSVC is just telling you the exit value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top