unexpected exception问题

zhangxiaoping 2003-07-27 10:57:42
c++ primer plus中关于意外异常有以下解说:


通过set_unecpected函数设置出现意外异常时的处理函数,处理函数可以引发新的异常:
如果新引发的异常与原来的异常规范匹配,则程序将从哪里开始执行,即寻找与新引发的异常相匹配的

catch块。
如果新引发的异常与原来的异常规范不匹配,且原来的异常规范中包含了std::bad_exception类型,则

不匹配的异常将被std::bad_exception异常所取代。

照上所述,以下两段代码应该正常运行,可实际却都异常终止了,why??

#include <exception>
#include <new>
#include <iostream>
using namespace std;

void fn(int i) throw(int)
{
if (i) cout<<"calling fn()!";
else throw float();
}

void myUnexpect()
{
throw int();//抛出int,被捕获??
}

void main()
{
set_unexpected(myUnexpect);
try {
fn(0);
}
catch(int)
{
cout<<"catching int!";
}
}



#include <exception>
#include <new>
#include <iostream>
using namespace std;

void fn(int i) throw(int,bad_exception)
{
if (i) cout<<"calling fn()!";
else throw float();
}

void myUnexpect()
{
throw char();
}

void main()
{
set_unexpected(myUnexpect);
try {
fn(0);
}
catch(int)
{
cout<<"catching int!";
}
catch(bad_exception)
{
cout<<"catching bad_exception!";
}
}
...全文
964 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxiaoping 2003-07-28
  • 打赏
  • 举报
回复
哎,辛苦理解了半天,结果却说不支持,悲哀啊,计算机
pengzhenwanli 2003-07-28
  • 打赏
  • 举报
回复
Establishes a new unexpected_handler to be when an unexpected exception is encountered.

unexpected_handler
set_unexpected(
unexpected_handler _Pnew
) throw( );
Parameters
_Pnew
The function to be called at
Return Value
The address of the previous unexpected_handler.

Remarks
_Pnew must not be a null pointer.

The C++ Standard requires that unexpected is called when a function throws an exception that is not on its throw list. The current implementation does not support this. The following example calls unexpected directly, which then calls the unexpected_handler.

Example
// exception_set_unexpected.cpp
// compile with: /EHsc
#include<exception>
#include<iostream>

using namespace std;

void unfunction( )
{
cout << "I'll be back." << endl;
terminate( );
}

int main( )
{
unexpected_handler oldHand = set_unexpected( unfunction );
unexpected( );
}
Output
I'll be back.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

上面是MSDN的说明The current implementation does not support this.
这每一句,说是现在的版本不支持这种用法

64,664

社区成员

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

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