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

后端 未结 6 1685
庸人自扰
庸人自扰 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 <stdio.h>
    #include <signal.h>
    #include <stdlib.h>
    
    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.

    0 讨论(0)
  • 2021-01-18 17:31

    It might be that VM tests the divisor for 0 manually in emulated bytecode (for simplicity of implementation), but for performance will still switch to detecting the SIGFPE signal in JIT'd code. Try putting the division code in its own subroutine and call it in a loop thousands of times to ensure it gets compiled.

    0 讨论(0)
  • 2021-01-18 17:33

    in java there is The term exception is shorthand for the phrase "exceptional event."
    An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
    When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
    if u catch that exception then u can do what u wants to do after catching exception otherwise it is handle by default handler which show the error info and terminate the program ...
    Dividing by zero in java make as ArithmeticException

    Exception in thread "main" java.lang.ArithmeticException: / by zero 
    
    0 讨论(0)
  • 2021-01-18 17:42

    In contrast to (normal) C compile program, Java program runs on a runtime, and not on the processor, and is not platform dependent. Dividing by zero in java triggers ArithmeticException like this:

    Exception in thread "main" java.lang.ArithmeticException: / by zero
    

    From JLS:

    An exception is thrown for one of three reasons:

    An abnormal execution condition was synchronously detected by the Java virtual machine. Such conditions arise because:

    evaluation of an expression violates the normal semantics of the language, such as an integer divide by zero, as summarized in §15.6

    an error occurs in loading or linking part of the program (§12.2, §12.3)

    some limitation on a resource is exceeded, such as using too much memory

    0 讨论(0)
  • 2021-01-18 17:46

    Here, it raises a SIGFPE.

    You forgot to tell strace to follow children. Add the -f option to strace and you should see something similar to:

    [pid  2304] read(3, "\312\376\272\276\0\0\0001\0n\n\0\23\0I\t\0\3\0J\7\0K\n\0L\0M\n\0N\0"..., 2369) = 2369
    [pid  2304] --- SIGFPE (Floating point exception) @ 0 (0) ---
    [pid  2304] rt_sigreturn(0x1c50800)     = 5
    [pid  2304] write(2, "Exception in thread \"main\" ", 27Exception in thread "main" ) = 27
    [pid  2304] write(2, "java.lang.ArithmeticException: /"..., 40java.lang.ArithmeticException: / by zero) = 40
    [pid  2304] write(2, "\n", 1
    
    0 讨论(0)
  • 2021-01-18 17:46

    Java runs in a virtual machine (the JVM), which abstracts the hardware from the program. Most errors in a Java program will cause Java Exceptions and not trigger any native cpu or os error codes. I'd guess this code will throw an ArithmeticException (or something like that).

    0 讨论(0)
提交回复
热议问题