From 02e723549bfcdd02526fd7e7256a6d130d3c671e Mon Sep 17 00:00:00 2001
From: Vasileios Karakasis <karakasis@cscs.ch>
Date: Fri, 29 Jan 2016 20:44:37 +0100
Subject: [PATCH] Added an swc_parse_error exception.

This exception is thrown when a parse error occurs, instead of
std::runtime_error and std::logical_error that used to be thrown before.
---
 src/swcio.hpp        | 26 +++++++++++++++++++-------
 tests/test_swcio.cpp |  4 ++--
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/swcio.hpp b/src/swcio.hpp
index ff605500..1687fa60 100644
--- a/src/swcio.hpp
+++ b/src/swcio.hpp
@@ -166,6 +166,18 @@ private:
     int parent_id_;     // cell parent's id
 };
 
+class swc_parse_error : public std::runtime_error
+{
+public:
+    explicit swc_parse_error(const char *msg)
+        : std::runtime_error(msg)
+    { }
+
+    explicit swc_parse_error(const std::string &msg)
+        : std::runtime_error(msg)
+    { }
+};
+
 class swc_parser
 {
 public:
@@ -199,7 +211,7 @@ public:
             return is;
 
         if (is.fail())
-            throw std::runtime_error("too long line detected");
+            throw swc_parse_error("too long line detected");
 
         std::istringstream line(linebuff_);
         cell = parse_record(line);
@@ -211,8 +223,7 @@ private:
     {
         if (is.fail())
             // If we try to read past the eof; fail bit will also be set
-            // FIXME: better throw a custom parse_error exception
-            throw std::logic_error("could not parse value");
+            throw swc_parse_error("could not parse value");
     }
 
     template<typename T>
@@ -270,15 +281,16 @@ std::istream &operator>>(std::istream &is, cell_record &cell)
     return is;
 }
 
+
 std::ostream &operator<<(std::ostream &os, const cell_record &cell)
 {
     // output in one-based indexing
     os << cell.id_+1 << " "
        << cell.type_ << " "
-       << std::setprecision(7) << cell.x_    << " "
-       << std::setprecision(7) << cell.y_    << " "
-       << std::setprecision(7) << cell.z_    << " "
-       << std::setprecision(7) << cell.r_    << " "
+       << std::setprecision(7) << cell.x_ << " "
+       << std::setprecision(7) << cell.y_ << " "
+       << std::setprecision(7) << cell.z_ << " "
+       << std::setprecision(7) << cell.r_ << " "
        << ((cell.parent_id_ == -1) ? cell.parent_id_ : cell.parent_id_+1);
 
     return os;
diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp
index 00310bbf..923f39b4 100644
--- a/tests/test_swcio.cpp
+++ b/tests/test_swcio.cpp
@@ -111,14 +111,14 @@ TEST(swc_parser, invalid_input)
         // check incomplete lines; missing parent
         std::istringstream is("1 1 14.566132 34.873772 7.857000 0.717830\n");
         cell_record cell;
-        EXPECT_THROW(is >> cell, std::logic_error);
+        EXPECT_THROW(is >> cell, swc_parse_error);
     }
 
     {
         // Check non-parsable values
         std::istringstream is("1a 1 14.566132 34.873772 7.857000 0.717830 -1\n");
         cell_record cell;
-        EXPECT_THROW(is >> cell, std::logic_error);
+        EXPECT_THROW(is >> cell, swc_parse_error);
     }
 
     {
-- 
GitLab