如何捕捉在C语言中的整数除零例外? [英] How to catch the integer division-by-zero exception in C language?
问题描述
据了解,如何捕捉到浮动师被零除异常带的使用
It is known how to catch the float division-by-zero exception with the usage of
signal(SIGFPE, handler)
,但它不即使赶上整数除法被零问题
but it doesn't catch integer division-by-zero problem even if I setup control word with
_control87(0, _MCW_EM ); (MS VC 2010)
SubQuestion_1:如何捕捉整数除零的C程序在Windows中没有SEH EXCEPTION_INT_DIVIDE_BY_ZERO的使用情况如何? (在Unix / Linux下可以使用标准的信号/ SIGFPE techinque的使用来实现)
SubQuestion_1: How to catch integer division-by-zero in C program in Windows without usage of SEH EXCEPTION_INT_DIVIDE_BY_ZERO? (In Unix/Linux this can be done with usage of the standard signal/SIGFPE techinque)
编辑:
信号是ANSI C的信号处理方法。
_control87是标准的Windows功能设置浮动控制字。
signal is ANSI C signal handling approach.
_control87 is the standard Windows function to set float control word.
类似的问题:<一href=\"http://stackoverflow.com/questions/2155442/how-to-handle-all-errors-including-internal-c-library-errors-uniformly\">http://stackoverflow.com/questions/2155442/how-to-handle-all-errors-including-internal-c-library-errors-uniformly
请注意(从ISO / IEC 9899:TC2附件H.2.2):
NOTE (from ISO/IEC 9899:TC2 Annex H.2.2):
的署名Ç整数类型int,long int和long long int和相应的
无符号的类型兼容LIA-1。 。C的无符号整数类型在LIA-1感''模'在溢出或超出范围的结果默默地换行。该定义符号整数类型,也被模不需要检测整数溢出的实现,在这种情况下,的仅整数除以零需要被检测到。的
"The signed C integer types int, long int, long long int, and the corresponding unsigned types are compatible with LIA−1. ... C’s unsigned integer types are ‘‘modulo’’ in the LIA−1 sense in that overflows or out-of-bounds results silently wrap. An implementation that defines signed integer types as also being modulo need not detect integer overflow, in which case, only integer divide-by-zero need be detected."
最终的解决方案:
对于Windows:它会抛出异常SEH。因此,它可以通过__try __except的使用被捕获。
作为可能的解决方案SEH翻译可以用来处理SEH异常,并翻译它们调用所需的功能。这不是一个自然的方式,但它似乎是唯一的办法。
For Windows: it throws SEH exception. So it can be caught by usage of __try __except. As possible solution SEH translation could be used to handle SEH exception and translate them to call of needed function. It is not a "natural" way, but it seems that it's the only way.
对于Unix:它可以与信号/ SIGFPE解决方案被抓住。或检查维基FPE_INTDIV溶液( HTTP://rosetta$c$c.org/wiki/ Detect_division_by_zero·C )。
For Unix: it could be caught with signal/SIGFPE solution. Or check wiki for FPE_INTDIV solution ( http://rosettacode.org/wiki/Detect_division_by_zero#C ).
由于GMAN是对的未定义的行为我选择他的答案是正确的。
As GMan was right about "undefined behaviour" I'm choosing his answer as correct.
注:有趣的是,查询VC \\ CRT \\ SRC \\ winxfltr.c:_XcptActTab数组:)
Note: It is interesting to check VC\crt\src\winxfltr.c: _XcptActTab array : )
推荐答案
被零除导致不确定的行为,没有C语言结构,可以做任何事情。最好的办法是零摆在首位,以不分,通过检查分母。
Division by zero leads to undefined behavior, there is no C language construct that can do anything about it. Your best bet is to not divide by zero in the first place, by checking the denominator.
如果你想钓(注意,C有没有例外)这个错误,这将取决于你的编译器和操作系统,你还没有上市。顺便说一句, _control87
只有做浮点运算,并没有做整数运算。
If you want to "catch" (note, C has no exceptions) this error, it will be dependent on your compiler and OS, which you haven't listed. By the way, _control87
has only to do with floating-point operations, and nothing to do with integer operations.
这篇关于如何捕捉在C语言中的整数除零例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!