added exception handling at the c/c++ boundary
diff --git a/src/cpc_sketch_c_adapter.cpp b/src/cpc_sketch_c_adapter.cpp
index f7c3674..bbe8ca9 100644
--- a/src/cpc_sketch_c_adapter.cpp
+++ b/src/cpc_sketch_c_adapter.cpp
@@ -15,58 +15,98 @@
 #include <cpc_union.hpp>
 
 void* cpc_sketch_new(unsigned lg_k) {
-  return new (palloc(sizeof(datasketches::cpc_sketch))) datasketches::cpc_sketch(lg_k, datasketches::DEFAULT_SEED, &palloc, &pfree);
+  try {
+    return new (palloc(sizeof(datasketches::cpc_sketch))) datasketches::cpc_sketch(lg_k, datasketches::DEFAULT_SEED, &palloc, &pfree);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void cpc_sketch_delete(void* sketchptr) {
-  static_cast<datasketches::cpc_sketch*>(sketchptr)->~cpc_sketch();
-  pfree(sketchptr);
+  try {
+    static_cast<datasketches::cpc_sketch*>(sketchptr)->~cpc_sketch();
+    pfree(sketchptr);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void cpc_sketch_update(void* sketchptr, const void* data, unsigned length) {
-  static_cast<datasketches::cpc_sketch*>(sketchptr)->update(data, length);
+  try {
+    static_cast<datasketches::cpc_sketch*>(sketchptr)->update(data, length);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 double cpc_sketch_get_estimate(const void* sketchptr) {
-  return static_cast<const datasketches::cpc_sketch*>(sketchptr)->get_estimate();
+  try {
+    return static_cast<const datasketches::cpc_sketch*>(sketchptr)->get_estimate();
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void cpc_sketch_to_string(const void* sketchptr, char* buffer, unsigned length) {
-  std::stringstream s;
-  s << *(static_cast<const datasketches::cpc_sketch*>(sketchptr));
-  snprintf(buffer, length, s.str().c_str());
+  try {
+    std::stringstream s;
+    s << *(static_cast<const datasketches::cpc_sketch*>(sketchptr));
+    snprintf(buffer, length, s.str().c_str());
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void* cpc_sketch_serialize(const void* sketchptr) {
   // intermediate copy in unavoidable with the current cpc_sketch API
   // potential improvement
-  std::stringstream s;
-  static_cast<const datasketches::cpc_sketch*>(sketchptr)->serialize(s);
-  unsigned length = s.tellp();
-  bytea* buffer = (bytea*) palloc(length + VARHDRSZ);
-  SET_VARSIZE(buffer, length + VARHDRSZ);
-  s.read(VARDATA(buffer), length);
-  return buffer;
+  try {
+    std::stringstream s;
+    static_cast<const datasketches::cpc_sketch*>(sketchptr)->serialize(s);
+    unsigned length = s.tellp();
+    bytea* buffer = (bytea*) palloc(length + VARHDRSZ);
+    SET_VARSIZE(buffer, length + VARHDRSZ);
+    s.read(VARDATA(buffer), length);
+    return buffer;
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void* cpc_sketch_deserialize(const char* buffer, unsigned length) {
   // intermediate copy in unavoidable with the current cpc_sketch API
   // potential improvement
-  std::stringstream s;
-  s.write(buffer, length);
-  auto ptr = datasketches::cpc_sketch::deserialize(s, datasketches::DEFAULT_SEED, &palloc, &pfree);
-  return ptr.release();
+  try {
+    std::stringstream s;
+    s.write(buffer, length);
+    auto ptr = datasketches::cpc_sketch::deserialize(s, datasketches::DEFAULT_SEED, &palloc, &pfree);
+    return ptr.release();
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void* cpc_union_new(unsigned lg_k) {
-  return new (palloc(sizeof(datasketches::cpc_union))) datasketches::cpc_union(lg_k, datasketches::DEFAULT_SEED, &palloc, &pfree);
+  try {
+    return new (palloc(sizeof(datasketches::cpc_union))) datasketches::cpc_union(lg_k, datasketches::DEFAULT_SEED, &palloc, &pfree);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void cpc_union_update(void* unionptr, const void* sketchptr) {
-  static_cast<datasketches::cpc_union*>(unionptr)->update(*static_cast<const datasketches::cpc_sketch*>(sketchptr));
+  try {
+    static_cast<datasketches::cpc_union*>(unionptr)->update(*static_cast<const datasketches::cpc_sketch*>(sketchptr));
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void* cpc_union_get_result(void* unionptr) {
-  auto ptr = static_cast<datasketches::cpc_union*>(unionptr)->get_result();
-  return ptr.release();
+  try {
+    auto ptr = static_cast<datasketches::cpc_union*>(unionptr)->get_result();
+    return ptr.release();
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
diff --git a/src/kll_float_sketch_c_adapter.cpp b/src/kll_float_sketch_c_adapter.cpp
index 5c77584..efc84ed 100644
--- a/src/kll_float_sketch_c_adapter.cpp
+++ b/src/kll_float_sketch_c_adapter.cpp
@@ -13,53 +13,93 @@
 typedef datasketches::kll_sketch<float, palloc_allocator<void>> kll_float_sketch;
 
 void* kll_float_sketch_new(unsigned k) {
-  return new (palloc(sizeof(kll_float_sketch))) kll_float_sketch(k);
+  try {
+    return new (palloc(sizeof(kll_float_sketch))) kll_float_sketch(k);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void kll_float_sketch_delete(void* sketchptr) {
-  static_cast<kll_float_sketch*>(sketchptr)->~kll_float_sketch();
-  pfree(sketchptr);
+  try {
+    static_cast<kll_float_sketch*>(sketchptr)->~kll_float_sketch();
+    pfree(sketchptr);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void kll_float_sketch_update(void* sketchptr, float value) {
-  static_cast<kll_float_sketch*>(sketchptr)->update(value);
+  try {
+    static_cast<kll_float_sketch*>(sketchptr)->update(value);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void kll_float_sketch_merge(void* sketchptr1, const void* sketchptr2) {
-  static_cast<kll_float_sketch*>(sketchptr1)->merge(*static_cast<const kll_float_sketch*>(sketchptr2));
+  try {
+    static_cast<kll_float_sketch*>(sketchptr1)->merge(*static_cast<const kll_float_sketch*>(sketchptr2));
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 double kll_float_sketch_get_rank(const void* sketchptr, float value) {
-  return static_cast<const kll_float_sketch*>(sketchptr)->get_rank(value);
+  try {
+    return static_cast<const kll_float_sketch*>(sketchptr)->get_rank(value);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 float kll_float_sketch_get_quantile(const void* sketchptr, double rank) {
-  return static_cast<const kll_float_sketch*>(sketchptr)->get_quantile(rank);
+  try {
+    return static_cast<const kll_float_sketch*>(sketchptr)->get_quantile(rank);
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void kll_float_sketch_to_string(const void* sketchptr, char* buffer, unsigned length) {
-  std::stringstream s;
-  s << *(static_cast<const kll_float_sketch*>(sketchptr));
-  snprintf(buffer, length, s.str().c_str());
+  try {
+    std::stringstream s;
+    s << *(static_cast<const kll_float_sketch*>(sketchptr));
+    snprintf(buffer, length, s.str().c_str());
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void kll_float_sketch_serialize(const void* sketchptr, char* buffer) {
   // intermediate copy in unavoidable with the current kll_sketch API
   // potential improvement
-  std::stringstream s;
-  static_cast<const kll_float_sketch*>(sketchptr)->serialize(s);
-  s.read(buffer, s.tellp());
+  try {
+    std::stringstream s;
+    static_cast<const kll_float_sketch*>(sketchptr)->serialize(s);
+    s.read(buffer, s.tellp());
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 void* kll_float_sketch_deserialize(const char* buffer, unsigned length) {
   // intermediate copy in unavoidable with the current kll_sketch API
   // potential improvement
-  std::stringstream s;
-  s.write(buffer, length);
-  auto ptr = kll_float_sketch::deserialize(s);
-  return ptr.release();
+  try {
+    std::stringstream s;
+    s.write(buffer, length);
+    auto ptr = kll_float_sketch::deserialize(s);
+    return ptr.release();
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }
 
 unsigned kll_float_sketch_get_serialized_size_bytes(const void* sketchptr) {
-  return static_cast<const kll_float_sketch*>(sketchptr)->get_serialized_size_bytes();
+  try {
+    return static_cast<const kll_float_sketch*>(sketchptr)->get_serialized_size_bytes();
+  } catch (std::exception& e) {
+    elog(ERROR, e.what());
+  }
 }