How do exceptions work from an Operating System's perspective?
Coming from C++, I can understand exceptions from a programmer's perspective.
When an exception gets thrown, the stack begins to unwind, and each activation record has the opportunity to catch and handle the exception.
But whose responsibility was it to throw the exception in the first place?
Here are two crashing programs that illustrate my uncertainty.
int main(){
int i = 1/0; //did the OS tell the process to end?
return 0;
}
#include <exception>
int main(){
throw 11; //did the process tell the OS it needs to end?
return 0;
}
Abstract. Exception handling is a powerful abstraction that can be used to help manage errors and support the construction of reliable operating systems. Using exceptions to notify system components about exceptional conditions also reduces coupling of error handling code and increases the modularity of the system.
The one responsible for catching and handling exceptions is the computer program. In Java, “an exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions” (Oracle, n., par.
Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.
Exception Handling in Java is one of the effective means to handle the runtime errors so that the regular flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
You're talking about two completely different exception processes.
The first is provided by the OS. In Windows you can use __try
and __except
to handle them.
The second is provided by the C++ compiler and doesn't involve the OS in any way.
C++ exceptions are part of the language, defined by the language standard, and implemented by the compiler and runtime library. There are other exceptions
that are detected by the CPU, like divide by zero or dereferencing a NULL pointer, both are examples of Undefined Behavior in the language standard. Those are faults
in processor terminology and on x86 for example trigger a fault handler
which is then serviced by the OS. The OS can then choose to report that fault to the process that caused it, on Unix this is done with signals
. If your process has installed a signal handler
for SIGSEGV
for example, it can handle the fault generated by the CPU when the process dereferences a NULL pointer... that mechanism is separate from the C++ exceptions defined by the language.
In your example, when a C++ program throws
an exception this is entirely handled by the compiler generated code and the language runtime library, there is no kernel call necessary and there is no hardware fault generated by the processor.
Since I'm only aware of one or two OS's written in C++, and the one I know better, doesn't officially use exceptions at all, that pretty much rules out exceptions being thrown by the OS.
The three main OS's (Linux, Windows, MacOS X), along with all forms of Unix (AIX, Solaris, HP-UX, etc) are written in C, along with almost any other commercially available OS that isn't written in assembler, so can not throw C++ type exceptions [that's not saying there aren't software driven exceptions, just that they are not the type of excepion you catch with a "try/catch" in C++ without some sort of translation].
In the first example, the OS is definitely involved [in all OS's that I know how they work], since division by zero causes a hardware exception on all machines that have division as a function, and thus the OS will need to be involved. Also, this will compile and fail in the same manner whether it is C++, C or you write the same thing in assembler. For most Operating Systems, they'll send a signal to the program, but since you have no code for handling signals, your code will most likely simply abort, telling the OS that something weird happened and it's giving up, not even bothering to unwind the stack.
In the second case, the OS is not at all involved. There is a "try-catch" block around the call to main, which says "Oops, someone threw something that wasn't caught, lets exit". The only part of that that involves the OS is the "exit this process", which of course will need to be done by the OS, although I do believe in most OS's, just returning from the 'start address of the application' will also have the same effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With