try catch为什么没有捕获到除0异常

nostopping 2010-01-10 09:46:58
代码如下:

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

void Crash(void)
{
int i = 1;
int j = 0;

try
{
i /= j;
//cout << i << endl; //cout the value

}
catch(...)
{
cout << "Abnormal in Crash" << endl;
}
//cout << i << endl;

printf("No abnormal in Crash\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
int i = 1;
int j = 0;

try
{
Crash();
}
catch(float i)
{
cout << "Error FLOAT:" << i << endl;
}
catch(int i)
{
cout << "Error INT:" << i << endl;
}
catch(...)
{
cout << "Abnormal in Main" << endl;
}

cout << "No Abnormal in Main" << endl;

getchar();
return 0;
}


问题是:
1.程序运行时为什么没有捕获到异常,输出为:
No abnormal in Crash
No Abnormal in Main
2.如果把Crash函数中注释掉的两个cout语句(或任意一个)打开,程序运行时系统会报出异常对话框,为何程序没有捕获到?
3.是不是每个异常都需要在程序中throw?
4.要捕获异常系统抛出的异常是否需要在编译器中设置什么选项(VS2008)?
...全文
1998 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxDreamer 2011-02-18
  • 打赏
  • 举报
回复
今天看c++ primer plus遇到这个问题vs2010试过了,
属性页 > 配置属性 > C/C++ > 代码生成 > 启用C++异常 = 是,但有 SEH 异常 (/EHa)

另外这里有个msdn的编译器选项表。
http://msdn.microsoft.com/zh-cn/library/fwkeyyhe.aspx
traceless 2010-01-12
  • 打赏
  • 举报
回复
学习了
cnzdgs 2010-01-12
  • 打赏
  • 举报
回复
除0是SEH异常。在项目属性—C/C++—代码生成中将“启用C++异常”设置为“是,但有SEH异常(/EHa)”。
cocat 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 healer_kx 的回复:]
捕获 除0 异常要看什么编译器的,据我说知,一般VC都可以,gcc都不行。
[/Quote]
还有这样的啊?真没试验过,受教!
  • 打赏
  • 举报
回复
顶帖是美德!
young2000 2010-01-11
  • 打赏
  • 举报
回复
受教了
healer_kx 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 healer_kx 的回复:]
捕获 除0 异常要看什么编译器的,据我说知,一般VC都可以,gcc都不行。
[/Quote]

我试验过,而且有一本专门讲过这点。
dqdx_zch 2010-01-11
  • 打赏
  • 举报
回复
应该在子函数里抛出异常:
if(j == 0)
throw runtime_error("jjjj");
这样,try catch才能捕获到的。
cattycat 2010-01-11
  • 打赏
  • 举报
回复
up甘草,忘了说了,我的确实是vc6.0的,没在gcc上试。
healer_kx 2010-01-11
  • 打赏
  • 举报
回复
捕获 除0 异常要看什么编译器的,据我说知,一般VC都可以,gcc都不行。
不说害怕 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 nostopping 的回复:]
问题是:
1.程序运行时为什么没有捕获到异常,输出为:
No abnormal in Crash
No Abnormal in Main
2.如果把Crash函数中注释掉的两个cout语句(或任意一个)打开,程序运行时系统会报出异常对话框,为何程序没有捕获到?
3.是不是每个异常都需要在程序中throw?
4.要捕获异常系统抛出的异常是否需要在编译器中设置什么选项(VS2008)?
[/Quote]
1. 异常能捕获到的...第一句应该是 abnormal in Crash 才对.
2. 你代码其他地方可能有问题.
3. 异常是自己发生的,自己throw是另一回事.
4. 默认就抛出异常的.
借我那把枪吧 2010-01-11
  • 打赏
  • 举报
回复
支持(加菲猫)的[Quote=引用 3 楼 cattycat 的回复:]
我这里捕获到异常了。
你的Crash里,有catch,除0后输出 Abnormal in Crash,但后面又跟着输出No abnormal in Crash,明显不对么,catch到异常输出后应该返回。
C/C++ codevoid Crash(void)
{int i=1;int j=0;try
{
i/= j;//cout << i << endl;//cout the value
}catch(...)
{
cout<<"Abnormal in Crash"<< endl;return;//要返回,就不输出printf那句了 }//cout << i << endl;
printf("No abnormal in Crash\n");
}
因为在Crash里catch了,所以main中不会在捕捉到异常,所以最后的输出是main中No Abnormal in Main.
[/Quote]
sj13426074890 2010-01-11
  • 打赏
  • 举报
回复


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

void Crash(void)
{
int i = 1;
int j = 0;

try
{
i /= j;
//cout << i << endl; //cout the value

}
catch(...)
{
cout << "Abnormal in Crash" << endl;
throw ;//mark
return ;

}
//cout << i << endl;

printf("No abnormal in Crash\n");
}

int _tmain(int argc, _TCHAR* argv[])
{
int i = 1;
int j = 0;

try
{
Crash();
}
catch(float i)
{
cout << "Error FLOAT:" << i << endl;
}
catch(int i)
{
cout << "Error INT:" << i << endl;
}
catch(...)
{
cout << "Abnormal in Main" << endl;
return 0;
}

cout << "No Abnormal in Main" << endl;

getchar();
return 0;
}





不处理 可以throw 在main函数就能捕获了
lovesi3344 2010-01-11
  • 打赏
  • 举报
回复
mark

if(y == 0)
throw divzero();

catch()
  • 打赏
  • 举报
回复
除0异常 是系统异常,不是C++异常
catch不能捕获也没啥奇怪的。
nostopping 2010-01-11
  • 打赏
  • 举报
回复
谢谢各位的回复,我再写个测试代码试下,Thanks!
IT_lau 2010-01-11
  • 打赏
  • 举报
回复
里面都catch了
外面调用还要catch?
beginnow 2010-01-10
  • 打赏
  • 举报
回复
首先你要明确几个问题:
1. try catch throw被称为C++异常,而__try __except __finally被称为C异常也叫SEH异常机制,wndows内嵌的就是SEH异常机制, 当windows的编译器编译c++的异常如try,catch的时候最终使用的仍然是SEH异常机制
2. SEH异常机制支持硬件异常和软件异常,而C++异常机制不支持硬件异常,而 EXCEPTION_ACCESS_VIOLATION
EXCEPTION_BREAKPOINT
EXCEPTION_FLT_DIVIDE_BY_ZERO等等都属于硬件异常,所以C++异常机制是捕获不到的
3.如果你确实想用c++异常机制破获硬件异常,必须在所有的硬件异常发生之前调用_set_se_translator这个函数,传入一个能够将所有硬件异常转换为C++异常机制的函数地址就可以了,
herman~~ 2010-01-10
  • 打赏
  • 举报
回复
mark
cattycat 2010-01-10
  • 打赏
  • 举报
回复
补充一点,异常是按调用栈顺序逐级返回的,在中间某一层catch对异常进行了处理,就不再向上抛出异常了,如果到了最后的main中,没有处理异常的,就会出现你说的红色对话框警告,操作系统捕捉了这个异常,你的程序终止。
加载更多回复(5)

64,654

社区成员

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

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