如何捕获异常:Integer division by zero

forcal 2009-09-18 04:34:01
以下程序,当输入0时,捕获不到异常,为什么?

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i;
char ch;

cin>>i;
try{
i=8/i;
}
catch(...)
{
cout<<"****异常***"<<endl;
}
cout<<"i="<<i<<endl;
cin>>ch;
return 0;
}
...全文
1678 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolcoffee4051982 2009-09-20
  • 打赏
  • 举报
回复
不是 C++ 异常。
闲梦远的天空 2009-09-20
  • 打赏
  • 举报
回复
上溢,下溢,除零都不会抛出标准异常,需要自己去判断,不然结果是无定义的。
ttchenwei 2009-09-18
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i;
char ch;

cin>>i;
try{
if(0==i)
throw i;
i=8/i;
}
catch(i)
{
cout<<"****异常***"<<endl;
}
cout<<"i="<<i<<endl;
cin>>ch;
return 0;
}
HelloAldis 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 thy38 的回复:]
#include "stdafx.h"
#include <windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i;

cin>>i;
__try
{
i=8/i;
cout<<"i="<<i<<endl;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
cout<<"****异常***"<<endl;
}

system("pause");
return 0;
}[/Quote]
没试过 试试
呵呵我平时 如果些小程序 懒得就这样写


#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i;
char ch;

cin>>i;
try{
if(i == 0)
{
throw i;
}
else
{
i=8/i;
}
}
catch(int e)
{
cout<<"****异常***"<<endl;
}
cout<<"i="<<i<<endl;
cin>>ch;
return 0;
}
waide__q 2009-09-18
  • 打赏
  • 举报
回复
要不自己写个类,再用throw抛出
幽雾 2009-09-18
  • 打赏
  • 举报
回复
可以捕获啊。。。


#include "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
int i;
char ch;

cin>>i;
try{
i=8/i;
}
catch(...)
{
cout<<"****异常***"<<endl;
}
cout<<"i="<<i<<endl;
cin>>ch;

return 0;
}
luotuo512 2009-09-18
  • 打赏
  • 举报
回复
去看看《软件加密技术内幕》,其中有讲到构造除0异常处理,SEH。
whg01 2009-09-18
  • 打赏
  • 举报
回复
这个会触发CPU的中断。想捕获这个,仔细研究研究操作系统和CPU中断。
有了结果告诉我一下。^_^
xzh_endless 2009-09-18
  • 打赏
  • 举报
回复
这个绝对叫异常
thinkboy234 2009-09-18
  • 打赏
  • 举报
回复
学习~~~
jean7155 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yshuise 的回复:]
这不属于c++异常错误。
[/Quote]
那属于什么啊?给个出处好去查( ⊙ o ⊙ )啊!
thy38 2009-09-18
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i;

cin>>i;
__try
{
i=8/i;
cout<<"i="<<i<<endl;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
cout<<"****异常***"<<endl;
}

system("pause");
return 0;
}
sunnywyg 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wolf0403 的回复:]
不是 C++ 异常。
[/Quote]
Wolf0403 2009-09-18
  • 打赏
  • 举报
回复
不是 C++ 异常。
yshuise 2009-09-18
  • 打赏
  • 举报
回复
这不属于c++异常错误。

65,203

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧