⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exception.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Exception class implementation
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"
#include "exception.h"

#include <errno.h>
#include <string>
#include <sstream>

using std::string;
using std::ostringstream;


// ============================================================================
// Construction
// ============================================================================

Exception::Exception(const string & reason)
   : line_number(0), file_name(""), message(reason), error_text("")
{
}

// ============================================================================
// Get error details
// ============================================================================

string Exception::get_error()
{
   ostringstream s;

   s << "Problem!\n\n";
   s << message << "\n";

   if (error_text != "") {
      s << error_text << "\n";
   }

   if (line_number != 0) {
      s << "\nError detected in module " << file_name 
        << " at line number " << line_number << "\n";
   }

   return s.str();
}


// ============================================================================
// Obtain the text corresponding to a windows error
// ============================================================================

string get_windows_error(DWORD error_code)
{
   char * message_buffer;
 
   FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      0,
      error_code,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
      reinterpret_cast<LPTSTR>(&message_buffer),
      0,
      0);

   string error = message_buffer;
   LocalFree(message_buffer);

   return error;
}


// ============================================================================
// Win_exeception construction
//
// Gets the information held in "last error"
// ============================================================================

Win_exception::Win_exception(const string & reason)
   : Exception(reason)
{
   error_text = get_windows_error(GetLastError());
}


// ============================================================================
// Win_exeception construction
//
// Allows the error code to be specified.
// ============================================================================

Win_exception::Win_exception(string const & reason,
                             DWORD  error_code)
   : Exception(reason)
{
   error_text = get_windows_error(error_code);
}


// ============================================================================
// Posix_exeception construction
// ============================================================================

Posix_exception::Posix_exception(const string & reason)
   : Exception(reason)
{
   error_text = strerror(errno);
}

⌨️ 快捷键说明

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