C ++:捕捉除以零错误 [英] C++ : Catch a divide by zero error

查看:120
本文介绍了C ++:捕捉除以零错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个简单的代码,其中发生除以零。我想抓住它:

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;
}

但是应用程序崩溃了(即使我把选项<$ c $

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) ?

我知道我可以检查之前除法,假设,因为除以零是罕见的(至少在我的应用程序中),它是更有效的尝试分割(和捕获错误,如果发生)比测试每次除数之前分裂。

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计算机上进行这些测试,但希望使其跨平台。

I'm doing these tests on a WindowsXP computer, but would like to make it cross platform.

推荐答案

这不是一个例外。这是在硬件级别确定的错误,并返回到操作系统,然后操作系统以某种特定于操作系统的方式通知您的程序(例如,该过程)。

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).

相信在这种情况下,会发生什么不是一个例外,而是一个信号。如果是这样:操作系统中断程序的主控制流程,并调用信号处理程序,这反过来会终止程序的操作。

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.

这是相同的

您可以尝试使用<$ c中的函数,这些错误会在您解除引用空指针时出现(然后您的程序因SIGSEGV信号崩溃而导致分段错误) $ c>< csignal> 头来尝试为SIGFPE信号提供一个自定义处理程序(这是针对浮点异常的,但也可能是这样的情况:真的不确定这里)。然而,您应该注意,信号处理是依赖于操作系统的,MinGW在Windows环境下以某种方式模拟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;
}

输出:

Signal 8 here!

Signal 8 here!

执行信号处理程序后,

And right after executing the signal handler, the system kills the process and displays an error message.

使用此方法,您可以关闭任何资源或在除以零或空指针引用之后记录错误...但与例外不同这不是一个方法来控制您的程序的流程,即使在特殊情况下。一个有效的程序不应该这样做。捕获这些信号只能用于调试/诊断目的。

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.

(有些有用的信号在低级编程中非常有用, t

这篇关于C ++:捕捉除以零错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆