Running the following code still produces an error message that goes to stdout (not stderr) although the exception was successfully caught:
Mat<double> matrix_quantiles(const vector<double> & quantiles,
const Mat<double> & m) {
Mat<double> sorted_matrix;
try {
sorted_matrix = arma::sort(arma::cor(m));
} catch(std::logic_error & e) {
/*
Sometimes a col is constant, causing the correlation to be
infinite. If that happens, add normal random jitter to the
values and retry.
*/
const Mat<double> jitter = Mat<double>(
m.n_rows, m.n_cols, arma::fill::randn);
return matrix_quantiles(quantiles, 1.e-3 * jitter + m);
}
etc.
The error message is:
error: sort(): given object has non-finite elements
The code runs fine, and the jittering strategy is good enough for what I do, but I have to filter out the error message if I write the output to stdout.
Thank you.
To disable printing of error messagaes, define a macro named ARMA_DONT_PRINT_ERRORS
before including the Armadillo header. For example:
#define ARMA_DONT_PRINT_ERRORS
#include <armadillo>
来源:https://stackoverflow.com/questions/31547767/armadillo-how-to-get-rid-of-error-message