0

I have been told that C does not have any runtime mechanism to catch exceptions like in Java. I am a bit confused about how, say for example, the division by zero exception is handled. I was reading about the IEEE 754 floating point standard and how it defines a division by zero exception, among others. So my questions is when I get something like this after running a program:

 $ ./divby0_demo
Floating point exception (core dumped)

Is this the OS/kernel killing my program (after "catching the exception") by sending something like a signal (like SEGV when a bad pointer dereference happens) or is this being handled by standard C libraries (libc) that my program gets linked with?

Sneftel
  • 40,271
  • 12
  • 71
  • 104
First User
  • 704
  • 5
  • 12
  • 2
    It can be implemented anywhere. Typically in a library, used by a language. – ryyker Aug 09 '21 at 12:26
  • 2
    @JonathanLeffler hardware may raise the exception, handling is up to : 1. OS 2. Libraries 3. User code. – 0___________ Aug 09 '21 at 12:29
  • Notice that the C standard says that divide by zero is undefined, i.e. maybe the result will just be "INF" (or even 42.0) without any exception being raised. – Support Ukraine Aug 09 '21 at 12:44
  • Does this answer your question? [The behaviour of floating point division by zero](https://stackoverflow.com/questions/42926763/the-behaviour-of-floating-point-division-by-zero) – SergeyA Aug 09 '21 at 13:09
  • 1
    Keep in mind that the word "exception" is used in three separate ways by (1) the IEEE-754 standard, (2) the CPU, (3) languages like C++ and Java. An "exception" in sense (1) is not meant to "throw" like sense (3), and it may or may not result in a CPU exception in sense (2), which may or may not raise a signal. – Nate Eldredge Aug 09 '21 at 14:55
  • Are you sure your program isn't generating an exception for integer division? At least on x86, you get the same SIGFPE from this too. – Brett Hale Aug 09 '21 at 15:45

1 Answers1

2

Well, you can implement it directly, if you like. That's not unheard-of: once upon a time, hardware support for floating point operations was uncommon, and even now it's uncommon in embedded processors.

IEEE-754 defines a set of data types and their interpretations, and a set of operations and their rules. The implementation of these can be done in the processor; in the code sequences generated by a compiler; in libraries used by an application; or in the application itself. In the most common case, the responsibilities are split between the processor (which directly implements many of the common operations), the compiler (which implements several) and the libraries (which implement the rest).

When a program crashes with an error about a floating point exception, that's largely the processor's doing. It detects the error and informs the OS by generating a particular interrupt signal. (The OS, compiler, libraries, and/or application may have put the CPU in a mode where it generates the interrupt signal rather than ignoring the situation and producing a NaN result; that's a more implementation-specific thing.) The OS' only real involvement is to formally terminate the program and print the error message. (The OS was not the entity attempting to do the division.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104