|
// ============================================================================
// 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);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.