This is not a C++ question, because there is no way to do so in standard C++.
In Windows, this generates a Structured Exception Handling exception.
See this example from MSDN:
__try
{
*pResult = dividend / divisor;
}
__except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
return FALSE;
}
This is non-portable and specific to Windows, though. Last I knew GCC doesn't support it either, even on Windows.
In case you are asking a broader question, the way it works is your CPU will generate an interrupt when you do a divide by zero. The Windows kernel handles this and delivers it to your process, giving you an opportunity to handle it. If you don't handle it your process dies. On Unix type OSs the same will hold, but it will probably result in a signal. I am not sure which one.
I would say if you want to write portable C++ code it's probably better to check for divide by zero in software rather than rely on hardware to generate a fault and for the OS kernel to deliver it to you via some non-portable mechanism.