0

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.

has981
  • 425
  • 4
  • 18

1 Answers1

2

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.

mah
  • 39,056
  • 9
  • 76
  • 93
  • I could verify that it is working fine by having another printf statement just before the return 0; and have a break point on it. The cause of my confusion is that for some reason, the break point placed inside the default case wasn't hit (although "Unknown error" was printed). Thanks for the help – has981 May 06 '13 at 10:28