Skip to content

Commit

Permalink
Move Exceptions into another file, add special Nan error
Browse files Browse the repository at this point in the history
  • Loading branch information
dguest committed Jan 18, 2017
1 parent 8dfdeb4 commit b566df1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
23 changes: 23 additions & 0 deletions include/ndhist/Exceptions.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef NDHIST_EXCEPTIONS
#define NDHIST_EXCEPTIONS

#include <stdexcept>
#include <string>

// exceptions
class HistogramSaveError: public std::runtime_error {
public:
HistogramSaveError(const std::string&);
};

class HistogramBinningError: public std::logic_error {
public:
HistogramBinningError(const std::string&);
};

class HistogramNanError: public HistogramBinningError {
public:
HistogramNanError(const std::string&);
};

#endif
6 changes: 0 additions & 6 deletions include/ndhist/Histogram.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <string>
#include <map>
#include <initializer_list>
#include <stdexcept>

namespace H5 {
class CommonFG;
Expand Down Expand Up @@ -103,11 +102,6 @@ private:
std::string m_wt2_ext;
};

// exceptions
class HistogramSaveError: public std::runtime_error {
public:
HistogramSaveError(const std::string&);
};

// global swap
void swap(Histogram&, Histogram&);
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ LIBS += -lhdf5_cpp -lhdf5

# ---- define objects
# - not-python
GEN_OBJ := Histogram.o Binners.o h5tools.o Distribution.o
GEN_OBJ := Histogram.o Binners.o h5tools.o Distribution.o Exceptions.o

# - command line interface
EXE_OBJ := test.o
Expand Down
10 changes: 6 additions & 4 deletions src/Binners.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Binners.hh"
#include <stdexcept>
#include "Exceptions.hh"
#include <algorithm>
#include <cmath>

Expand Down Expand Up @@ -56,7 +56,8 @@ int LinBinner::get_bin(const std::map<std::string, double>& locator) const
typedef std::map<std::string, double> DMap;
DMap::const_iterator bin_itr = locator.find(m_name);
if (bin_itr == locator.end()) {
throw std::runtime_error("could not find " + m_name + " in values given");
throw HistogramBinningError(
"could not find " + m_name + " in values given");
}
double value = bin_itr->second;
int bin = get_bin(value);
Expand All @@ -73,7 +74,8 @@ int LinBinner::get_bin(const std::vector<double>& locator, size_t offset)
{
int index = locator.size() - offset - 1;
if (index < 0) {
throw std::runtime_error("could not find " + m_name + " in values given");
throw HistogramBinningError(
"could not find " + m_name + " in values given");
}
double value = locator.at(index);
int bin = get_bin(value);
Expand All @@ -88,7 +90,7 @@ int LinBinner::get_bin(const std::vector<double>& locator, size_t offset)
int LinBinner::get_bin(double value) const
{
if (std::isnan(value)) {
throw std::range_error(m_name + " binner was passed NaN");
throw HistogramNanError(m_name + " binner was passed NaN");
}
int bin = 0;
if (value < m_low) {
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Exceptions.hh"

//______________________________________________________________________
// exception definitions

HistogramSaveError::HistogramSaveError(const std::string& what):
std::runtime_error(what)
{}

HistogramBinningError::HistogramBinningError(const std::string& what):
std::logic_error(what)
{}

HistogramNanError::HistogramNanError(const std::string& what):
HistogramBinningError(what)
{}
8 changes: 1 addition & 7 deletions src/Histogram.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "h5tools.hh"
#include "Binners.hh"
#include "Exceptions.hh"

#include <stdexcept>
#include <set>
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -318,12 +318,6 @@ namespace {

}

//______________________________________________________________________
// exception definitions

HistogramSaveError::HistogramSaveError(const std::string& what):
std::runtime_error(what)
{}

// ____________________________________________________________________
// swap
Expand Down
1 change: 1 addition & 0 deletions src/test.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Histogram.hh"
#include "Binners.hh"
#include "Distribution.hh"
#include "Exceptions.hh"
#include "H5Cpp.h"
#include <vector>

Expand Down

0 comments on commit b566df1

Please sign in to comment.