如何捕获算术异常? [英] how to catch the Arithmetic exception?

查看:94
本文介绍了如何捕获算术异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕捉算术异常,但未成功。

尝试{

int a = 0,b = 9;

b = b / a;

} catch(...){

cout<< 捕获了算术异常! <<结束;

}


运行程序后,它退出了核心转储。

%test

算术异常(核心转储)


谁可以告诉我一个合适的方法?

I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?

推荐答案

Gary .Hu写道:
Gary.Hu wrote:
我试图抓住算术异常,但没有成功。
尝试{
int a = 0,b = 9;
b = b / a;
} catch(...){
cout<< 捕获了算术异常! <<运行程序后,它退出了核心转储。
%test
算术异常(核心转储)

谁可以告诉我一个合适的方法?
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?




当其他所有方法都失败时,为什么不试试

catch(std :: exception& ; e){

std :: cout<< e.what()<< std :: endl;

}

它可能是类型std :: runtime_error的例外。


-

Emacs是一个不错的操作系统,但我更喜欢UNIX。



When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
It''s probably an exception of type std::runtime_error.

--
Emacs is a nice OS, but I prefer UNIX.


" Jacques Labuschagne" < JA ***** @ clawshrimp.com>写在消息

news:1168213.OcskaVrlgv@klesk ...
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:1168213.OcskaVrlgv@klesk...
Gary.Hu写道:
Gary.Hu wrote:
我试图捕获算术异常,但没有成功。
尝试{
int a = 0,b = 9;
b = b / a;
} catch(...){
cout<< 捕获了算术异常! <<运行程序后,它退出了核心转储。
%test
算术异常(核心转储)

谁可以告诉我一个合适的方法?
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?



当其他所有方法都失败时,为什么不试试
catch(std :: exception& e){
std :: cout<< e.what()<< std :: endl;
}



When all else fails, why not try
catch(std::exception& e){
std::cout << e.what() << std::endl;
}




是否有任何理由为什么catch(...)不会捕获异常

由catch(std :: exception& e)捕获......?


算术异常可能是一个操作系统或处理器异常,它与b ++异常并不相同,因此无法通过

try / catch块捕获。如果你问我需要平台和/或编译器

特定结构来处理算术异常。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl



Is there any reason why catch(...) wouldn''t catch exceptions that are
catched by catch(std::exception& e)...?

"Arithmetic exception" is probably an OS or processor exception which is
not the same as a C++ exception, hence it can not be catched by a
try/catch block. If you ask me you need platform and/or compiler
specific constructs to handle "Arithmetic exceptions".

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


2003年7月21日星期一14: 45:28 -0700,Gary.Hu < hu **** @ metarnet.com>

在comp.lang.c ++中写道:
On Mon, 21 Jul 2003 14:45:28 -0700, "Gary.Hu" <hu****@metarnet.com>
wrote in comp.lang.c++:
我试图捕获算术异常,但未成功。
尝试{
int a = 0,b = 9;
b = b / a;
} catch(...){
cout<< ; 捕获了算术异常! <<运行程序后,它退出了核心转储。
%test
算术异常(核心转储)

谁可以告诉我一个合适的方法?
I was trying to catch the Arithmetic exception, unsuccessfully.
try{
int a = 0, b = 9;
b = b / a;
}catch(...){
cout << "arithmetic exception was catched!" << endl;
}

After ran the program, it quitted with core dumped.
%test
Arithmetic exception (core dumped)

who can tell me an appropriate approach?




这种类型的算术错误不是C ++异常,它们是未定义的行为。您的编译器可能会提供一种方法将它们转换为C ++异常,但不是必需的。


对可能发生的事情没有任何要求或保证当你

产生未定义的行为时。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c http:/ /www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang .learn.c-c ++ ftp://snurse-l.org/ pub / acllc -c ++ / faq



Arithmetic errors of this type are not C++ exceptions, they are
undefined behavior. Your compiler might provide a way to turn them
into C++ exceptions, but it is not required to.

There are no requirements or guarantees on what might happen when you
generate undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


这篇关于如何捕获算术异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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