From ef9158364b18aed42ca0a1f9dbe038d11131d6e9 Mon Sep 17 00:00:00 2001
From: "A. Maitland Bottoms" <bottoms@debian.org>
Date: Fri, 14 Jan 2022 18:11:56 -0500
Subject: [PATCH 4/4] shared library throw not exit

In the case of an error, the library should instead return an
appropriate error code to the calling program which can then determine
how to handle the error, including performing any required
clean-up. In C++ the error can be thrown.
---
 gr-fec/lib/alist.cc                 |  4 ++--
 gr-fec/lib/conv_bit_corr_bb_impl.cc |  2 +-
 gr-fec/lib/fec_mtrx_impl.cc         | 18 +++++++++---------
 gr-trellis/lib/interleaver.cc       |  3 +--
 gr-video-sdl/lib/sink_s_impl.cc     |  2 +-
 gr-video-sdl/lib/sink_uc_impl.cc    |  2 +-
 6 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/gr-fec/lib/alist.cc b/gr-fec/lib/alist.cc
index bc93fa366..a2ba80632 100644
--- a/gr-fec/lib/alist.cc
+++ b/gr-fec/lib/alist.cc
@@ -119,8 +119,8 @@ void alist::read(const char* fname)
 void alist::write(const char* fname) const
 {
     if (!data_ok) {
-        std::cout << "Data not ok, exiting" << std::endl;
-        exit(1);
+        std::cout << "Data not ok, alist::write returning" << std::endl;
+        return;
     }
     // Else
     std::ofstream file(fname, std::ofstream::out);
diff --git a/gr-fec/lib/conv_bit_corr_bb_impl.cc b/gr-fec/lib/conv_bit_corr_bb_impl.cc
index 81146506e..16f898bd9 100644
--- a/gr-fec/lib/conv_bit_corr_bb_impl.cc
+++ b/gr-fec/lib/conv_bit_corr_bb_impl.cc
@@ -244,7 +244,7 @@ float conv_bit_corr_bb_impl::data_garble_rate(int taps, float target)
 
     if ((errno == EDOM) || (errno == ERANGE)) {
         GR_LOG_ERROR(d_logger, "Out of range errors while computing garble rate.");
-        exit(-1);
+	throw std::runtime_error("conv_bit_corr_bb_impl::data_garble_rate");
     }
     return answer;
 }
diff --git a/gr-fec/lib/fec_mtrx_impl.cc b/gr-fec/lib/fec_mtrx_impl.cc
index a9caa91cb..847aeecc8 100644
--- a/gr-fec/lib/fec_mtrx_impl.cc
+++ b/gr-fec/lib/fec_mtrx_impl.cc
@@ -301,13 +301,13 @@ void fec_mtrx_impl::add_matrices_mod2(gsl_matrix* result,
 
     if (matrix1_rows != matrix2_rows) {
         std::cout << "Error in add_matrices_mod2. Matrices do"
-                  << " not have the same number of rows.\n";
-        exit(1);
+		  << " not have the same number of rows.\n";
+	throw std::runtime_error("fec_mtrx::add_matrices_mod2");
     }
     if (matrix1_cols != matrix2_cols) {
         std::cout << "Error in add_matrices_mod2. Matrices do"
-                  << " not have the same number of columns.\n";
-        exit(1);
+		  << " not have the same number of columns.\n";
+	throw std::runtime_error("fec_mtrx::add_matrices_mod2");
     }
 
     // Copy matrix1 into result
@@ -338,11 +338,11 @@ void fec_mtrx_impl::mult_matrices_mod2(gsl_matrix* result,
     unsigned int d = (*matrix2).size2; // # of columns
     if (b != c) {
         std::cout << "Error in "
-                  << "fec_mtrx_impl::mult_matrices_mod2."
-                  << " Matrix dimensions do not allow for matrix "
-                  << "multiplication operation:\nmatrix1 is " << a << " x " << b
-                  << ", and matrix2 is " << c << " x " << d << ".\n";
-        exit(1);
+		  << "fec_mtrx_impl::mult_matrices_mod2."
+		  << " Matrix dimensions do not allow for matrix "
+		  << "multiplication operation:\nmatrix1 is " << a << " x " << b
+		  << ", and matrix2 is " << c << " x " << d << ".\n";
+	throw std::runtime_error("fec_mtrx::mult_matrices_mod2");
     }
 
     // Perform matrix multiplication. This is not mod 2.
diff --git a/gr-trellis/lib/interleaver.cc b/gr-trellis/lib/interleaver.cc
index cc7f8b327..547a8bcb6 100644
--- a/gr-trellis/lib/interleaver.cc
+++ b/gr-trellis/lib/interleaver.cc
@@ -136,8 +136,7 @@ void interleaver::write_interleaver_txt(std::string filename)
 {
     std::ofstream interleaver_fname(filename.c_str());
     if (!interleaver_fname) {
-        std::cout << "file not found " << std::endl;
-        exit(-1);
+	throw std::runtime_error("interleaver::write_interleaver(std::string filename): file not found error");
     }
     interleaver_fname << d_K << std::endl;
     interleaver_fname << std::endl;
diff --git a/gr-video-sdl/lib/sink_s_impl.cc b/gr-video-sdl/lib/sink_s_impl.cc
index 25fbd077e..e489593bf 100644
--- a/gr-video-sdl/lib/sink_s_impl.cc
+++ b/gr-video-sdl/lib/sink_s_impl.cc
@@ -84,7 +84,7 @@ sink_s_impl::sink_s_impl(
         msg << "Unable to set SDL video mode: " << SDL_GetError()
             << "; SDL_SetVideoMode() Failed";
         GR_LOG_ERROR(d_logger, msg.str());
-        exit(1);
+        throw std::runtime_error("video_sdl::sink_s");
     }
     if (d_image) {
         SDL_FreeYUVOverlay(d_image);
diff --git a/gr-video-sdl/lib/sink_uc_impl.cc b/gr-video-sdl/lib/sink_uc_impl.cc
index 5da2926ba..14c490947 100644
--- a/gr-video-sdl/lib/sink_uc_impl.cc
+++ b/gr-video-sdl/lib/sink_uc_impl.cc
@@ -84,7 +84,7 @@ sink_uc_impl::sink_uc_impl(
         msg << "Unable to set SDL video mode: " << SDL_GetError()
             << "; SDL_SetVideoMode() Failed";
         GR_LOG_ERROR(d_logger, msg.str());
-        exit(1);
+        throw std::runtime_error("video_sdl::sink_uc");
     }
 
     if (d_image) {
-- 
2.30.2

