問題描述
這是一段簡單的代碼,其中發(fā)生了被零除.我正試圖抓住它:
Here is a simple piece of code where a division by zero occurs. I'm trying to catch it :
#include <iostream>
int main(int argc, char *argv[]) {
int Dividend = 10;
int Divisor = 0;
try {
std::cout << Dividend / Divisor;
} catch(...) {
std::cout << "Error.";
}
return 0;
}
但是應(yīng)用程序還是崩潰了(即使我設(shè)置了 MinGW 的選項(xiàng) -fexceptions
).
But the application crashes anyway (even though I put the option -fexceptions
of MinGW).
是否有可能捕獲這樣的異常(我理解這不是 C++ 異常,而是 FPU 異常)?
Is it possible to catch such an exception (which I understand is not a C++ exception, but a FPU exception) ?
我知道我可以在除法之前檢查除數(shù),但我假設(shè),因?yàn)槌粤闶呛币姷?至少在我的應(yīng)用程序中),它會更多嘗試除法(并在發(fā)生錯誤時(shí)捕獲錯誤)比在除法之前每次測試除數(shù)更有效.
I'm aware that I could check for the divisor before dividing, but I made the assumption that, because a division by zero is rare (at least in my app), it would be more efficient to try dividing (and catching the error if it occurs) than testing each time the divisor before dividing.
我正在 WindowsXP 計(jì)算機(jī)上進(jìn)行這些測試,但希望使其跨平臺.
I'm doing these tests on a WindowsXP computer, but would like to make it cross platform.
推薦答案
這也不例外.這是一個錯誤,它在硬件級別確定并返回給操作系統(tǒng),然后操作系統(tǒng)以某種特定于操作系統(tǒng)的方式通知您的程序(例如,通過殺死過程).
It's not an exception. It's an error which is determined at hardware level and is returned back to the operating system, which then notifies your program in some OS-specific way about it (like, by killing the process).
我相信在這種情況下發(fā)生的不是異常而是信號.如果是這種情況:操作系統(tǒng)會中斷您程序的主控制流并調(diào)用信號處理程序,從而終止程序的運(yùn)行.
I believe that in such case what happens is not an exception but a signal. If it's the case: The operating system interrupts your program's main control flow and calls a signal handler, which - in turn - terminates the operation of your program.
這與取消引用空指針時(shí)出現(xiàn)的錯誤類型相同(然后您的程序因 SIGSEGV 信號而崩潰,分段錯誤).
It's the same type of error which appears when you dereference a null pointer (then your program crashes by SIGSEGV signal, segmentation fault).
您可以嘗試使用 <csignal>
標(biāo)頭中的函數(shù)來嘗試為 SIGFPE 信號提供自定義處理程序(它用于浮點(diǎn)異常,但也可能是這種情況整數(shù)除以零 - 我在這里真的不確定).但是,您應(yīng)該注意信號處理依賴于操作系統(tǒng),并且 MinGW 以某種方式模擬"了 Windows 環(huán)境下的 POSIX 信號.
You could try to use the functions from <csignal>
header to try to provide a custom handler for the SIGFPE signal (it's for floating point exceptions, but it might be the case that it's also raised for integer division by zero - I'm really unsure here). You should however note that the signal handling is OS-dependent and MinGW somehow "emulates" the POSIX signals under Windows environment.
這是在 MinGW 4.5、Windows 7 上的測試:
Here's the test on MinGW 4.5, Windows 7:
#include <csignal>
#include <iostream>
using namespace std;
void handler(int a) {
cout << "Signal " << a << " here!" << endl;
}
int main() {
signal(SIGFPE, handler);
int a = 1/0;
}
輸出:
信號 8 在這里!
在執(zhí)行信號處理程序后,系統(tǒng)立即終止進(jìn)程并顯示錯誤消息.
And right after executing the signal handler, the system kills the process and displays an error message.
使用此功能,您可以在除以零或空指針取消引用后關(guān)閉任何資源或記錄錯誤...但與異常不同,即使在特殊情況下,也不能控制程序流程. 一個有效的程序不應(yīng)該這樣做.捕獲這些信號僅用于調(diào)試/診斷目的.
Using this, you can close any resources or log an error after a division by zero or a null pointer dereference... but unlike exceptions that's NOT a way to control your program's flow even in exceptional cases. A valid program shouldn't do that. Catching those signals is only useful for debugging/diagnosing purposes.
(有一些有用的信號通常在低級編程中非常有用,并且不會導(dǎo)致您的程序在處理程序之后立即被殺死,但這是一個很深的話題).
這篇關(guān)于C++:捕捉除以零錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!