LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 698|回复: 1

异常

[复制链接]
发表于 2005-10-30 14:11:07 | 显示全部楼层 |阅读模式

  1. //program.cpp
  2. #include<iostream>

  3. int
  4. main()
  5. {
  6.    using namespace std;
  7.    char myarray[10];
  8.    
  9.    try{
  10.           for(int n = 0; n <= 10; n++){
  11.                    if(n > 9){
  12.                                   throw "out of range";
  13.                    }
  14.                    myarray[n] = 'z';
  15.            }
  16.     }

  17.    catch(char *str)
  18.    {
  19.        cout <<" excption:" << str << endl;
  20.     }
  21.    
  22.     return 0;
  23. }

  24. g++ program.cpp
  25. .a.out
  26. aborted

  27. >gdb a.out
  28. program received signal SIGABRT, Aborted
  29. 0x42029241 in kill() form /lib/i686/libc.so.6
  30. >bt
  31. #0  0x42029241 in kill() from /lib/i686/libc.so.6
  32. #1  0x4202922a in raise() from /lib/i686/libc.so.6
  33. #2  0x4202a7d2 in abort() form /lib/i686/libc.so.6
  34. #3  0x40055b8b in __default_terminate() at ../../gcc/libgcc2.c:-1
  35. #4  0x40055baa in __terminater() from /usr/lib/libstdc++-libc6.2-2.so.3
  36. #5   0x400568e5  in throw_helper (eh = 0x40066960,  pc = 0x42017498,  my_udata = 0xbffff9a0,  offset_p = 0xbffff9c)
  37.        from /usr/lib/libstdc++-libc6.2-2.so.3
  38. #6  0x4005660a  in __throw() from /usr/lib/libstdc++-libc6.2-2.so.3
  39. #7  0x08048843  in main()
  40. #8  0x42017499  in __libc__start_main() from /usr/lib/libstdc++-libc6.2-2.so.3
复制代码


初学C++,问题可能很幼稚,请大虾见亮
kill() 怎么会 发出 SIGABRT, 程序看来看去看不出毛病:(
发表于 2005-10-30 14:57:32 | 显示全部楼层
~/test$ g++ test_throw.cc
~/test$ ./a.out
terminate called after throwing an instance of 'char const*'
Aborted
~/test$


看起来是类型不符
改成catch (char const *str)后:
~/test$ ./a.out
excptionut of range
~/test$
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表