In java, “5/0” statement doesn't fire SIGFPE signal on my Linux machine, why?

后端 未结 6 1686
庸人自扰
庸人自扰 2021-01-18 17:04

I wrote a very simple c program:

#include

int main(){
    int a=2;
    int b=0;
    printf(\"%d\\n\", a/b);
}

and run it wi

6条回答
  •  星月不相逢
    2021-01-18 17:23

    Obviously this is because JVM has something like this in its code:

    #include 
    #include 
    #include 
    
    void fpe_handler(int signum) {
      printf("JVM throws an ArithmeticException here...\n");
      exit (1);
    }
    
    int main() {
      int a = 5;
      int b = 0;
      signal(SIGFPE, fpe_handler);
      printf("%d\n", a / b);
      return 0;
    }
    

    Also JVM runs multiple threads (see clone() in the log above or do ps -eLf when java is running) so that strace output is just incomplete.

    If a little more detail, unhandled SIGFPE indicates an error in the program, in which it occurred. And if java would be killed by SIGFPE, it would indicate than an error is in JVM, but not in your app, running inside JVM.

提交回复
热议问题