hello_world.cpp

来自「ace开发环境 用来开发网络程序 其运用了设计模式、多平台、C++等多种知识」· C++ 代码 · 共 141 行

CPP
141
字号
// file      : Example/ExH/HelloWorld/hello_world.cpp// author    : Boris Kolpackov <boris@kolpackov.net>// copyright : Copyright (c) 2002-2003 Boris Kolpackov// license   : http://kolpackov.net/license.html#include <cstdlib> // for std::abort ()#include <string>#include <iostream>#include "Utility/ExH/System/Exception.hpp"#include "Utility/ExH/Logic/Exception.hpp"using std::cerr;using std::cout;using std::endl;using namespace Utility;class Application{public:  class Exception : public ExH::Logic::Exception {};  // Hint: you may want to try again...  class FeelingDizzy  : public Exception {};  class InvalidArg : public Exception {};public:  Application ()      : // The std::string c-tor may throw any kind of exceptions besides        // quite possible std::bad_alloc.        greeting_ ("Hello, world!")  {  }  Application (char const * greeting)      : greeting_ (greeting == 0 ? "" : greeting)  {    if (greeting == 0) throw InvalidArg ();  }public:  void  run ()  {    static unsigned int dizzy_count (0);    if (dizzy_count++ < 5) throw FeelingDizzy ();    // The next line can throw full bucket of exceptions    // not to mention ios_base::failure.    cout << greeting_.c_str () << endl;  }private:  std::string  greeting_;};intmain (){  // This is a catch-all layer that should be in use only  // if we are really in trouble.  try  {    // This is a catch-system layer. Here we will catch exceptions like    // bad_alloc, etc. If we get here it means that nobody wanted/managed    // to recover from this kind of errors.    try    {      // This is a catch-logic layer. If we get here it usually      // indicates an application logic error.      try      {        // Ok, here we go about our application logic.        try        {          for (int i = 0; i < 10; i++)          {            try            {              Application app ("Hi dude!");              app.run ();              break;            }            catch (Application::FeelingDizzy const& )            {              if (i == 9)              {                cerr << "Given up!" << endl;                return -1;              }              else              {                cerr << "Application is feeling dizzy. Trying again..."                     << endl;              }            }          }        }        catch (Application::InvalidArg const& )        {          cerr << "Cought Application::InvalidArg : ...hmm... strange!"               << endl;          return -1;        }      }      catch (ExH::Logic::Exception const& e)      {        cerr << "Caught Logic::Exception : " << e.what () << endl;        return -1;      }    }    catch (const ExH::System::Exception& e)    {      cerr << "Caught System::Exception : " << e.what () << endl;      return -1;    }    catch (...)    {      cerr << "Caught unknown exception using catch-all handler. " << endl;      return -1;    }  }  catch (...)  {    // We get here in cases of some hard failure. For example when handling    // exception, operator << throws another exception. Usually application    // cannot handle such failures itself so we just propagate it futher.    std::abort ();  }}//$Id: hello_world.cpp 79112 2007-07-31 09:41:46Z sowayaa $

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?