免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4091 | 回复: 5
打印 上一主题 下一主题

Eclipse 的 run 与 Debug 为何执行结果不同? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-10 15:12 |只看该作者 |倒序浏览
//: C01:Unexpected.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Exception specifications & unexpected(),
//{-msc} (Doesn't terminate properly)
#include <exception>
#include <iostream>
using namespace std;

class Up {};
class Fit {};
void g();

void f(int i) throw(Up, Fit) {
  switch(i) {
    case 1: throw Up();
    case 2: throw Fit();
  }
  g();
}

// void g() {}         // Version 1
void g() { throw 47; } // Version 2

void my_unexpected() {
  cout << "unexpected exception thrown" << endl;
//  exit(0);
}

int main() {
  set_unexpected(my_unexpected); // (Ignores return value)
  for(int i = 1; i <=3; i++)
    try {
      f(i);
    } catch(Up) {
      cout << "Up caught" << endl;
    } catch(Fit) {
      cout << "Fit caught" << endl;
    }
} ///:~

Run时输出:(期望值)
Up caught
Fit caught
unexpected exception thrown

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

Debug时,第一次进入f(i)时
void f(int i) throw(Up, Fit) {
  switch(i) {
    case 1: throw Up();//本应该throw后跳出
    case 2: throw Fit();//但是Debug接着执行了这一行
  }
  g();//接着执行了这一行
}

论坛徽章:
0
2 [报告]
发表于 2006-03-11 09:34 |只看该作者
大虾们来看看!!!
:)

论坛徽章:
0
3 [报告]
发表于 2006-03-11 10:51 |只看该作者
我不知道C++中throw的机理,加个break试试

[ 本帖最后由 picktracy 于 2006-3-11 10:55 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-03-13 10:21 |只看该作者
加个break;就不必了吧,都已经throw了,因该立即调用set_unexpected里的函数才对!

论坛徽章:
0
5 [报告]
发表于 2006-03-13 12:55 |只看该作者
你是单布跟踪的吗?

当你发现它执行到

  1. case 2: throw Fit();//但是Debug接着执行了这一行
复制代码

时,
请察看一下此时的 i 的值~~~

论坛徽章:
0
6 [报告]
发表于 2006-03-14 09:30 |只看该作者
To:
白色乌鸦 (夏天到了)

多谢提醒,嘿嘿(咱俩是老乡)!

退栈时已经是异常了,故单步Debug已经不正常了,但在set_unexpected()函数设个断点,可以会执行的!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP