-1

I am running the below program :

#include <iostream>

using namespace std;

int main()
{

    while(1)
    {
        int a;
        cin >> a;
        cout << 9/a << endl; 
    }
    return 0;
}

the step by step process is

  1. the above code gets compiled and successively converted into binary fotmat.
  2. the binary code instructions get executed step by step.

so when I provide the input with : 0, I receive following on the console : Floating point exception: 8

Now my question is how does this "Floating point exception: 8" gets printed on console when machine is executing binary instructions.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
user1718009
  • 303
  • 3
  • 7
  • @Ron , plz explain where my assumption is wrong – user1718009 Mar 04 '18 at 10:30
  • "With yet even more binary codes", of course. Are you not sure about what happens inside when a program is "run"? – Jongware Mar 04 '18 at 10:30
  • It depends how you run it. An IDE will find exceptions and report them. From a prompt these are baked into the exe. When you invoke `\` there's a lot going on in there. – doctorlove Mar 04 '18 at 10:30
  • @doctorlove , I am running it with g++ file.cpp – user1718009 Mar 04 '18 at 10:32
  • @Ron , I do not think there is any relation between "binary nature of machines has anything to do with how runtime libraries format and display messages on standard output", my question is when an exception is occured on the machine level, what follows after that till I see "Floating point exception: 8" on the console – user1718009 Mar 04 '18 at 10:36
  • 3
    This mishap is detected by the processor, it generates a trap that is handled by the OS. Specific to Unix, it raises the SIGFPE signal and if the program doesn't handle it then the CRT does by terminating the program after displaying this message. https://www.gnu.org/software/libc/manual/html_node/Program-Error-Signals.html – Hans Passant Mar 04 '18 at 10:36
  • A huge load of this stuff is OS/driver/video/etc specifc. It would take a book to explain 'exactly'. Far too broad for SO. – Martin James Mar 04 '18 at 11:11

1 Answers1

0

The arithmetic and logic unit (ALU) of the CPU will contain a register of error flags. Divide by zero is one of those. The ALU will likely spot the zero in the denominator or choke and set the flag.

Either way, at the machine level an interrupt will occur, which is what we (eventually) see as an exception. It traps (via an interrupt vector) to a section of binary code for exception handling, which is where higher level exception handling is invoked from.

More detail's tend to be architecture dependent but could cause the pipeline to be invalidated.

John
  • 6,433
  • 7
  • 47
  • 82
  • at the machine level after the interrupt is occured, what exactly happens after that, till I see "Floating point exception: 8" on my console – user1718009 Mar 04 '18 at 10:34
  • I've explained from the machine level to the C/C++ interrupt handler. Explaining C++ at the machine level is the task of the debugger / compiler. See https://stackoverflow.com/questions/20842515/what-happens-when-i-divide-by-zero if I was not clear – John Mar 04 '18 at 10:41