2015-07-21 128 views
0

的運行下面的代碼仍然產生到stdout(不是標準錯誤)雖然異常被成功捕獲一條錯誤消息:犰狳如何擺脫錯誤信息

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. 

的錯誤信息是:

error: sort(): given object has non-finite elements 

代碼運行良好,抖動策略足以滿足我的要求,但如果將輸出寫入stdout,則必須過濾掉錯誤消息。

謝謝。

回答

1

要禁用打印錯誤信息,請在包含Armadillo標頭之前定義一個名爲ARMA_DONT_PRINT_ERRORS的宏。例如:

#define ARMA_DONT_PRINT_ERRORS 
#include <armadillo> 
+0

有關配置Armadillo的更多信息,請參見[documentation](http://arma.sourceforge.net/docs.html#config_hpp) – mtall