捕获异常:除以零 [英] Catching exception: divide by zero
问题描述
下面的代码不捕获异常,当我尝试除以0.我需要抛出一个异常,或者计算机在运行时自动抛出一个异常?
$
int i = 0;
cin>>一世; //如果有人输入零?
try {
i = 5 / i;
}
catch(std :: logic_error e){
cerr<< e.what();
}
并抛出异常。整数除以零在标准C ++中不是一个例外。
浮点除以零,但至少具有处理它的具体方法。
ISO标准中列出的例外情况如下:
namespace std {
class logic_error;
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class runtime_error;
class range_error;
class overflow_error;
class underflow_error;
}
你会认为 overflow_error $ c
,虽然我不认为这从以前的迭代改变了)具体说明:
如果
/
或%
的第二个操作数为零,则此行为未定义。
所以,它可以抛出那个(或任何其他)异常。它也可以格式化您的硬盘和笑嘲讽: - )
如果你想实现这样的野兽,在以下程序中使用 intDivEx
:
#include< iostream>
#include< stdexcept>
//整数除法,捕捉除以零。
inline int intDivEx(int numerator,int denominator){
if(denominator == 0)
throw std :: overflow_error(Divide by zero exception);
return numerator / denominator;
}
int main(void){
int i = 42;
try {
i = intDivEx(10,2);
} catch(std :: overflow_error e){
std :: cout< e.what()< - >;
}
std :: cout<< i<< std :: endl;
try {
i = intDivEx(10,0);
} catch(std :: overflow_error e){
std :: cout< e.what()< - >;
}
std :: cout<< i<< std :: endl;
return 0;
}
此输出:
5
除以零异常 - > 5
,你可以看到它抛出和捕获异常除以零的情况。 >
等效的%
几乎完全相同:
//整数余数,除以零。
inline int intModEx(int numerator,int denominator){
if(denominator == 0)
throw std :: overflow_error(Divide by zero exception);
return numerator%denominator;
}
The following code does not catch an exception, when I try to divide by 0. Do I need to throw an exception, or does the computer automatically throw one at runtime?
int i = 0;
cin >> i; // what if someone enters zero?
try {
i = 5/i;
}
catch (std::logic_error e) {
cerr << e.what();
}
You need to check it yourself and throw an exception. Integer divide by zero is not an exception in standard C++.
Neither is floating point divide by zero but at least that has specific means for dealing with it.
The exceptions listed in the ISO standard are:
namespace std {
class logic_error;
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class runtime_error;
class range_error;
class overflow_error;
class underflow_error;
}
and you would think that overflow_error
would be ideal for indicating a divide by zero.
But section 5.6
(of C++11
, though I don't think this has changed from the previous iteration) specifically states:
If the second operand of
/
or%
is zero, the behavior is undefined.
So, it could throw that (or any other) exception. It could also format your hard disk and laugh derisively :-)
If you wanted to implement such a beast, you could use something like intDivEx
in the following program:
#include <iostream>
#include <stdexcept>
// Integer division, catching divide by zero.
inline int intDivEx (int numerator, int denominator) {
if (denominator == 0)
throw std::overflow_error("Divide by zero exception");
return numerator / denominator;
}
int main (void) {
int i = 42;
try {
i = intDivEx (10, 2);
} catch (std::overflow_error e) {
std::cout << e.what() << " -> ";
}
std::cout << i << std::endl;
try {
i = intDivEx (10, 0);
} catch (std::overflow_error e) {
std::cout << e.what() << " -> ";
}
std::cout << i << std::endl;
return 0;
}
This outputs:
5
Divide by zero exception -> 5
and you can see it throws and catches the exception for the divide by zero case.
The %
equivalent is almost exactly the same:
// Integer remainder, catching divide by zero.
inline int intModEx (int numerator, int denominator) {
if (denominator == 0)
throw std::overflow_error("Divide by zero exception");
return numerator % denominator;
}
这篇关于捕获异常:除以零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!