Navigation Menu

Skip to content

Commit

Permalink
Add grnxx::File/Mmap/MemoryError.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 28, 2013
1 parent 87208b8 commit 5c880b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
42 changes: 33 additions & 9 deletions lib/grnxx/exception.hpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2012 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -22,26 +22,50 @@

#include <exception>

#define GRNXX_THROW() (throw ::grnxx::Exception())

namespace grnxx {

// The base exception class.
class Exception : std::exception {
public:
Exception() noexcept {}
virtual ~Exception() noexcept {}

Exception(const Exception &) noexcept {}
Exception &operator=(const Exception &) noexcept {
return *this;
virtual const char *what() const noexcept {
return "grnxx::Exception";
}
};

// This exception is thrown when a file operation fails.
class FileError : public Exception {
public:
FileError() noexcept {}
virtual ~FileError() noexcept {}

virtual const char *what() const noexcept {
return "grnxx::FileError";
}
};

// This exception is thrown when memory mapping fails.
class MmapError : public Exception {
public:
MmapError() noexcept {}
virtual ~MmapError() noexcept {}

virtual const char *what() const noexcept {
return "";
return "grnxx::MmapError";
}
};

private:
// TODO.
// This exception is thrown when memory allocation fails.
class MemoryError : public Exception {
public:
MemoryError() noexcept {}
virtual ~MemoryError() noexcept {}

virtual const char *what() const noexcept {
return "grnxx::MemoryError";
}
};

} // namespace grnxx
Expand Down
10 changes: 3 additions & 7 deletions test/test_exception.cpp
Expand Up @@ -28,17 +28,13 @@ int main() {
bool thrown = true;
bool catched = false;
try {
GRNXX_THROW();
throw grnxx::Exception();
thrown = false;
} catch (const grnxx::Exception &) {
} catch (const grnxx::Exception &exception) {
GRNXX_NOTICE() << "exception.what() = " << exception.what();
catched = true;
}

GRNXX_NOTICE() << "thrown = " << thrown;
GRNXX_NOTICE() << "catched = " << catched;

assert(thrown);
assert(catched);

return 0;
}

0 comments on commit 5c880b2

Please sign in to comment.