| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> C++ cheng xu de yi chang chu li ji qiao

C++ cheng xu de yi chang chu li ji qiao

2008-06-01 02:10:15 编辑來源:互联网 评论
 
 
 
本文为【C++程序的异常处理技巧】的拼音翻译版
  chu li C++ zhong de yi chang hui zai yu yan ji bie shang peng dao shao xu yin han xian zhi , dan zai mou xie qing kuang xia , nin ke yi rao guo ta men 。 xue xi ge zhong li yong yi chang de fang fa , nin jiu ke yi sheng chan geng ke kao de ying yong cheng xu 。
  bao liu yi chang lai yuan xin xi
  zai C++ zhong , wu lun he shi zai chu li cheng xu nei bu zhuo yi ge yi chang , guan yu gai yi chang lai yuan de xin xi dou shi bu wei ren zhi de 。 yi chang de ju ti lai yuan ke yi ti gong xu duo geng hao di chu li gai yi chang de chong yao xin xi , huo zhe ti gong yi xie ke yi fu jia dao cuo wu ri zhi de xin xi , yi bian yi hou jin xing fen xi 。
  wei le jie jue zhe yi wen ti , ke yi zai pao chu yi chang yu ju qi jian , zai yi chang dui xiang de gou zao han shu zhong sheng cheng yi ge dui zhan gen zong 。ExceptionTracer shi shi fan zhe zhong xing wei de yi ge lei 。
  qing dan 1. zai yi chang dui xiang gou zao han shu zhong sheng cheng yi ge dui zhan gen zong
  // Sample Program:
  // Compiler: gcc 3.2.3 20030502
  // Linux: Red Hat
  #include <execinfo.h>
  #include <signal.h>
  #include <exception>
  #include <iostream>
  using namespace std;
  /////////////////////////////////////////////
  class ExceptionTracer
  {
  public:
  ExceptionTracer()
  {
  void * array[25];
  int nSize = backtrace(array, 25);
  char ** symbols = backtrace_symbols(array, nSize);
  for (int i = 0; i < nSize; i++)
  {
  cout << symbols[i] << endl;
  }
  free(symbols);
  }
  };
  zhi li xin hao
  mei dang jin cheng zhi xing yi ge ling ren tao yan de dong zuo , yi zhi yu Linux? nei gai fa chu yi ge xin hao shi , gai xin hao dou bi xu bei chu li 。 xin hao chu li cheng xu tong chang hui shi fang yi xie chong yao zi yuan bing zhong zhi ying yong cheng xu 。 zai zhe zhong qing kuang xia , dui zhan shang de suo you dui xiang shi li dou chu yu wei po huai zhuang tai 。 ling yi fang mian , jia ru zhe xie xin hao bei zhuan huan cheng C++ yi chang , na me nin ke yi you ya di tiao yong qi gou zao han shu , bing an pai duo ceng catch kuai , yi bian geng hao di chu li zhe xie xin hao 。
  qing dan 2 zhong ding yi de SignalExceptionClass, ti gong le biao shi nei gai ke neng fa chu xin hao de C++ yi chang de chou xiang 。SignalTranslator shi yi ge ji yu SignalExceptionClass de mo ban lei , ta tong chang yong lai shi xian dao C++ yi chang de zhuan huan 。 zai ren he shun jian , zhi neng you yi ge xin hao chu li cheng xu chu li yi ge huo dong jin cheng de yi ge xin hao 。 yin ci ,SignalTranslator cai yong le singleton she ji mo shi 。 zheng ti gai nian tong guo yong yu SIGSEGV de SegmentationFault lei he yong yu SIGFPE de FloatingPointException lei de dao le zhan shi 。
  qing dan 2. jiang xin hao zhuan huan cheng yi chang
  template <class SignalExceptionClass> class SignalTranslator
  {
  private:
  class SingleTonTranslator
  {
  public:
  SingleTonTranslator()
  {
  signal(SignalExceptionClass::GetSignalNumber(),
  SignalHandler);
  }
  static void SignalHandler(int)
  {
  throw SignalExceptionClass();
  }
  };
  public:
  SignalTranslator()
  {
  static SingleTonTranslator s_objTranslator;
  }
  };
  // An example for SIGSEGV
  class SegmentationFault : public ExceptionTracer, public
  exception
  {
  public:
  static int GetSignalNumber() {return SIGSEGV;}
  };
  SignalTranslator<SegmentationFault>
  g_objSegmentationFaultTranslator;
  // An example for SIGFPE
  class FloatingPointException : public ExceptionTracer, public
  exception
  {
  public:
  static int GetSignalNumber() {return SIGFPE;}
  };
  SignalTranslator<FloatingPointException>
  g_objFloatingPointExceptionTranslator;
   zhi li gou zao han shu he xi gou han shu zhong de yi chang
  zai quan ju ( jing tai quan ju ) bian liang de gou zao he xi gou qi jian , mei ge ANSI C++ dou bu zhuo dao yi chang shi bu ke neng de 。 yin ci ,ANSI C++ bu jian yi zai na xie qi shi li ke neng bei ding yi wei quan ju shi li ( jing tai quan ju shi li ) de lei de gou zao han shu he xi gou han shu zhong pao chu yi chang 。 huan yi zhong shuo fa jiu shi yong yuan dou bu yao wei na xie qi gou zao han shu he xi gou han shu ke neng pao chu yi chang de lei ding yi quan ju ( jing tai quan ju ) shi li 。 bu guo , jia ru jia ding you yi ge te ding bian yi qi he yi ge te ding xi tong , na me ke neng ke yi zhe yang zuo , xing yun de shi , dui yu Linux shang de GCC, qia hao shi zhe zhong qing kuang 。
  shi yong ExceptionHandler lei ke yi zhan shi zhe yi dian , gai lei ye cai yong le singleton she ji mo shi 。 qi gou zao han shu zhu ce le yi ge wei bu zhuo de chu li cheng xu 。 yin wei mei ci zhi neng you yi ge wei bu zhuo de chu li cheng xu chu li yi ge huo dong jin cheng , gou zao han shu ying gai zhi bei tiao yong yi ci , yin ci yao cai yong singleton mo shi 。 ying gai zai ding yi you wen ti de shi ji quan ju ( jing tai quan ju ) bian liang zhi qian ding yi ExceptionHandler de quan ju ( jing tai quan ju ) shi li 。
  qing dan 3. chu li gou zao han shu zhong de yi chang
  class ExceptionHandler
  
  
  {
  private:
  class SingleTonHandler
  {
  public:
  SingleTonHandler()
  {
  set_terminate(Handler);
  }
  static void Handler()
  {
  // Exception from constrUCtion/destruction of global variables try
  {
  // re-throw throw;
  }
  catch (SegmentationFault &)
  {
  cout << “SegmentationFault” << endl;
  }
  catch (FloatingPointException &)
  {
  cout << “FloatingPointException” << endl;
  }
  catch (...)
  {
  cout << “Unknown Exception” << endl;
  }
  //if this is a thread performing some core activity
  abort();
  // else if this is a thread used to service requests
  // pthread_exit();
  }
  };
  public:
  ExceptionHandler()
  {
  static SingleTonHandler s_objHandler;
  }
  };
  //////////////////////////////////////////////////////////////////////////
  class A
  {
  public:
  A()
  {
  //int i = 0, j = 1/i;
  *(int *)0 = 0;
  }
  };
  // Before defining any global variable, we define a dummy instance
  // of ExceptionHandler object to make sure that
  // ExceptionHandler::SingleTonHandler::SingleTonHandler() is
  invoked
  ExceptionHandler g_objExceptionHandler;
  A g_a;
  //////////////////////////////////////////////////////////////////////////
  int main(int argc, char* argv[])
  {
  return 0;
  }
   chu li duo xian cheng cheng xu zhong de yi chang
  you shi yi xie yi chang mei you bei bu zhuo , zhe jiang zao cheng jin cheng yi chang zhong zhi 。 bu guo hen duo shi hou , jin cheng bao han duo ge xian cheng , qi zhong shao shu xian cheng zhi xing gai xin ying yong cheng xu luo ji , tong shi , qi yu xian cheng wei wai bu qing qiu ti gong fu wu 。 jia ru fu wu xian cheng yin bian cheng cuo wu er mei you chu li mou ge yi chang , ze hui zao cheng zheng ge ying yong cheng xu beng kui 。 zhe yi dian ke neng shi bu shou ren men huan ying de , yin wei ta hui tong guo xiang ying yong cheng xu chuan song bu he fa de qing qiu er zhu chang ju jue fu wu gong ji 。 wei le bi mian zhe yi dian , wei bu zhuo chu li cheng xu ke yi jue ding shi qing qiu yi chang zhong zhi tiao yong , huan shi qing qiu xian cheng tui chu tiao yong 。 qing dan 3 zhong ExceptionHandler::SingleTonHandler::Handler() han shu de mo yi chu zhan shi le gai chu li cheng xu 。
  jie shu yu
  wo jian dan di tao lun le shao xu C++ bian cheng she ji mo shi , yi bian geng hao di zhi xing yi xia ren wu :
  · zai pao chu yi chang de shi hou zhui zong yi chang de lai yuan 。
  · jiang xin hao cong nei gai cheng xu zhuan huan cheng C++ yi chang 。
  · bu zhuo gou zao he / huo xi gou quan ju bian liang qi jian pao chu de yi chang 。
  · duo xian cheng jin cheng zhong de yi chang chu li 。【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
chu li C++ zhong de yi chang hui zai yu yan ji bie shang peng dao shao xu yin han xian zhi , dan zai mou xie qing kuang xia , nin ke yi rao guo ta men 。 xue xi ge zhong li yong yi chang de fang fa , nin jiu ke yi sheng chan geng ke kao de ying yong cheng xu 。 bao liu yi chang lai yuan xin xi zai C++ zhong , wu lun he shi zai chu li cheng xu nei bu zhuo yi ge yi chang , guan yu gai yi chang lai yuan de xin xi dou shi bu wei ren zhi de 。 yi chang de ju ti lai yuan ke yi ti gong xu duo geng hao di chu li gai yi chang de chong yao xin xi , huo zhe ti gong yi xie ke yi fu jia dao cuo wu ri zhi de xin xi , yi bian yi hou jin xing fen xi 。 wei le jie jue zhe yi wen ti , ke yi zai pao chu yi chang yu ju qi jian , zai yi chang dui xiang de gou zao han shu zhong sheng cheng yi ge dui zhan gen zong 。ExceptionTracer shi shi fan zhe zhong xing wei de yi ge lei 。 qing dan 1. zai yi chang dui xiang gou zao han shu zhong sheng cheng yi ge dui zhan gen zong // Sample Program: // Compiler: gcc 3.2.3 20030502 // Linux: Red Hat #include <execinfo.h> #include <signal.h> #include <exception> #include <iostream> using namespace std; ///////////////////////////////////////////// class ExceptionTracer { public: ExceptionTracer() { void * array[25]; int nSize = backtrace(array, 25); char ** symbols = backtrace_symbols(array, nSize);   for (int i = 0; i < nSize; i++) { cout << symbols[i] << endl; } free(symbols); } }; zhi li xin hao mei dang jin cheng zhi xing yi ge ling ren tao yan de dong zuo , yi zhi yu Linux? nei gai fa chu yi ge xin hao shi , gai xin hao dou bi xu bei chu li 。 xin hao chu li cheng xu tong chang hui shi fang yi xie chong yao zi yuan bing zhong zhi ying yong cheng xu 。 zai zhe zhong qing kuang xia , dui zhan shang de suo you dui xiang shi li dou chu yu wei po huai zhuang tai 。 ling yi fang mian , jia ru zhe xie xin hao bei zhuan huan cheng C++ yi chang , na me nin ke yi you ya di tiao yong qi gou zao han shu , bing an pai duo ceng catch kuai , yi bian geng hao di chu li zhe xie xin hao 。 qing dan 2 zhong ding yi de SignalExceptionClass, ti gong le biao shi nei gai ke neng fa chu xin hao de C++ yi chang de chou xiang 。SignalTranslator shi yi ge ji yu SignalExceptionClass de mo ban lei , ta tong chang yong lai shi xian dao C++ yi chang de zhuan huan 。 zai ren he shun jian , zhi neng you yi ge xin hao chu li cheng xu chu li yi ge huo dong jin cheng de yi ge xin hao 。 yin ci ,SignalTranslator cai yong le singleton she ji mo shi 。 zheng ti gai nian tong guo yong yu SIGSEGV de SegmentationFault lei he yong yu SIGFPE de FloatingPointException lei de dao le zhan shi 。 qing dan 2. jiang xin hao zhuan huan cheng yi chang template <class SignalExceptionClass> class SignalTranslator { private: class SingleTonTranslator { public: SingleTonTranslator() { signal(SignalExceptionClass::GetSignalNumber(), SignalHandler); } static void SignalHandler(int) { throw SignalExceptionClass(); } }; public: SignalTranslator() { static SingleTonTranslator s_objTranslator; } }; // An example for SIGSEGV class SegmentationFault : public ExceptionTracer, public exception { public: static int GetSignalNumber() {return SIGSEGV;} }; SignalTranslator<SegmentationFault> g_objSegmentationFaultTranslator; // An example for SIGFPE class FloatingPointException : public ExceptionTracer, public exception { public: static int GetSignalNumber() {return SIGFPE;} }; SignalTranslator<FloatingPointException> g_objFloatingPointExceptionTranslator; zhi li gou zao han shu he xi gou han shu zhong de yi chang zai quan ju ( jing tai quan ju ) bian liang de gou zao he xi gou qi jian , mei ge ANSI C++ dou bu zhuo dao yi chang shi bu ke neng de 。 yin ci ,ANSI C++ bu jian yi zai na xie qi shi li ke neng bei ding yi wei quan ju shi li ( jing tai quan ju shi li ) de lei de gou zao han shu he xi gou han shu zhong pao chu yi chang 。 huan yi zhong shuo fa jiu shi yong yuan dou bu yao wei na xie qi gou zao han shu he xi gou han shu ke neng pao chu yi chang de lei ding yi quan ju ( jing tai quan ju ) shi li 。 bu guo , jia ru jia ding you yi ge te ding bian yi qi he yi ge te ding xi tong , na me ke neng ke yi zhe yang zuo , xing yun de shi , dui yu Linux shang de GCC, qia hao shi zhe zhong qing kuang 。 shi yong ExceptionHandler lei ke yi zhan shi zhe yi dian , gai lei ye cai yong le singleton she ji mo shi 。 qi gou zao han shu zhu ce le yi ge wei bu zhuo de chu li cheng xu 。 yin wei mei ci zhi neng you yi ge wei bu zhuo de chu li cheng xu chu li yi ge huo dong jin cheng , gou zao han shu ying gai zhi bei tiao yong yi ci , yin ci yao cai yong singleton mo shi 。 ying gai zai ding yi you wen ti de shi ji quan ju ( jing tai quan ju ) bian liang zhi qian ding yi ExceptionHandler de quan ju ( jing tai quan ju ) shi li 。 qing dan 3. chu li gou zao han shu zhong de yi chang class ExceptionHandler { private: class SingleTonHandler { public: SingleTonHandler() { set_terminate(Handler); } static void Handler() { // Exception from constrUCtion/destruction of global variables try { // re-throw throw; } catch (SegmentationFault &) { cout << “SegmentationFault” << endl; } catch (FloatingPointException &) { cout << “FloatingPointException” << endl; } catch (...) { cout << “Unknown Exception” << endl; } //if this is a thread performing some core activity abort(); // else if this is a thread used to service requests // pthread_exit(); } }; public: ExceptionHandler() { static SingleTonHandler s_objHandler; } }; ////////////////////////////////////////////////////////////////////////// class A { public: A() { //int i = 0, j = 1/i; *(int *)0 = 0; } }; // Before defining any global variable, we define a dummy instance // of ExceptionHandler object to make sure that // ExceptionHandler::SingleTonHandler::SingleTonHandler() is invoked ExceptionHandler g_objExceptionHandler; A g_a; ////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { return 0; } chu li duo xian cheng cheng xu zhong de yi chang you shi yi xie yi chang mei you bei bu zhuo , zhe jiang zao cheng jin cheng yi chang zhong zhi 。 bu guo hen duo shi hou , jin cheng bao han duo ge xian cheng , qi zhong shao shu xian cheng zhi xing gai xin ying yong cheng xu luo ji , tong shi , qi yu xian cheng wei wai bu qing qiu ti gong fu wu 。 jia ru fu wu xian cheng yin bian cheng cuo wu er mei you chu li mou ge yi chang , ze hui zao cheng zheng ge ying yong cheng xu beng kui 。 zhe yi dian ke neng shi bu shou ren men huan ying de , yin wei ta hui tong guo xiang ying yong cheng xu chuan song bu he fa de qing qiu er zhu chang ju jue fu wu gong ji 。 wei le bi mian zhe yi dian , wei bu zhuo chu li cheng xu ke yi jue ding shi qing qiu yi chang zhong zhi tiao yong , huan shi qing qiu xian cheng tui chu tiao yong 。 qing dan 3 zhong ExceptionHandler::SingleTonHandler::Handler() han shu de mo yi chu zhan shi le gai chu li cheng xu 。 jie shu yu wo jian dan di tao lun le shao xu C++ bian cheng she ji mo shi , yi bian geng hao di zhi xing yi xia ren wu : · zai pao chu yi chang de shi hou zhui zong yi chang de lai yuan 。 · jiang xin hao cong nei gai cheng xu zhuan huan cheng C++ yi chang 。 · bu zhuo gou zao he / huo xi gou quan ju bian liang qi jian pao chu de yi chang 。 · duo xian cheng jin cheng zhong de yi chang chu li 。
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
桌球宝贝_性感在蔓延(3)
桌球宝贝_性感在蔓延(2)
银杏树下清纯女孩(9)
银杏树下清纯女孩(8)
痞子的甘南日记
疑是银河落九天
雪域坝上四——纯美色
冬日恋歌——西城杨柳弄轻柔
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有