2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 14:05:33 +00:00

make isc::dns::Exception inherit from std::exception

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/jinmei-dnsmessageapi@421 e5f2f494-b856-4b98-b285-d166d9295462
This commit is contained in:
JINMEI Tatuya
2009-12-31 00:35:41 +00:00
parent a3c696d385
commit 67bcfeeb15
4 changed files with 86 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
lib_LTLIBRARIES = libdns.la
libdns_la_SOURCES = buffer.h name.cc name.h messagerenderer.h messagerenderer.cc
libdns_la_SOURCES += exceptions.h
libdns_la_SOURCES += exceptions.h exceptions.cc
TESTS =
if HAVE_GTEST

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// $Id$
#include <string>
#include "exceptions.h"
using isc::dns::Exception;
namespace isc {
namespace dns {
const char*
Exception::what() const throw()
{
const char* whatstr = "isc::dns::Exception";
// XXX: even though it's very unlikely that c_str() throws an exception,
// it's still not 100% guaranteed. To meet the exception specification
// of this function, we catch any unexpected exception and fall back to
// the pre-defined constant.
try {
whatstr = what_.c_str();
} catch (...) {
// no exception handling is necessary. just have to catch exceptions.
}
return (whatstr);
}
}
}

View File

@@ -17,6 +17,7 @@
#ifndef __EXCEPTIONS_H
#define __EXCEPTIONS_H 1
#include <stdexcept>
#include <string>
namespace isc {
@@ -29,7 +30,7 @@ namespace dns {
/// exception such as the file name and line number where the exception is
/// triggered.
///
class Exception {
class Exception : public std::exception {
public:
///
/// \name Constructors and Destructor
@@ -44,7 +45,7 @@ public:
Exception(const char* file, size_t line, const char* what) :
file_(file), line_(line), what_(what) {}
/// The destructor
virtual ~Exception() {}
virtual ~Exception() throw() {}
//@}
private:
///
@@ -53,6 +54,19 @@ private:
void operator=(const Exception& src);
public:
///
/// \name Methods Reimplemented against the Standard Exception Class
///
//@{
/// \brief Returns a C-style character string of the cause of the exception.
///
/// Note: we normally don't use exception specifications, but this is an
/// "exception" to that policy as it's enforced by the base class.
///
/// @return A C-style character string of the exception cause.
virtual const char* what() const throw();
//@}
///
/// \name Getter Methods
///
@@ -71,6 +85,7 @@ public:
///
/// @return an integer specifying the line number.
size_t getLine() const { return (line_); }
//@}
private:
const char* const file_;

View File

@@ -14,21 +14,39 @@
// $Id$
#include <stdexcept>
#include <string>
#include "exceptions.h"
#include <gtest/gtest.h>
namespace {
using isc::dns::Exception;
TEST(ExceptionTest, ExceptionTest) {
namespace {
class ExceptionTest : public ::testing::Test {
protected:
ExceptionTest() : teststring("test") {}
const char* teststring;
};
TEST_F(ExceptionTest, BasicMethods) {
try {
dns_throw(Exception, "test");
dns_throw(Exception, teststring);
} catch (Exception& ex) {
EXPECT_EQ(ex.getMessage(), "test");
EXPECT_EQ(ex.getMessage(), std::string(teststring));
EXPECT_EQ(ex.getFile(), std::string(__FILE__));
EXPECT_EQ(ex.getLine(), __LINE__ - 4);
}
}
// Test to see if it works as a proper derived class of std::exception.
TEST_F(ExceptionTest, StdInheritance) {
try {
dns_throw(Exception, teststring);
} catch (std::exception& ex) {
EXPECT_EQ(std::string(ex.what()), std::string(teststring));
}
}
}