Why can’t I catch this exception?
My (client) code:
Eigen::MatrixXd FFs ;
try
{
FFs.resize( NUMPATCHES, NUMPATCHES ) ;
}
catch( int e )
{
error( "Not enough memory :(" ) ;
return ;
}
The Eigen code that throws the exception is a few levels down..
EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
{
internal::check_rows_cols_for_overflow(rows, cols);
m_storage.resize(rows*cols, rows, cols);
}
Which calls
void resize(DenseIndex size, DenseIndex rows, DenseIndex cols)
{
if(size != m_rows*m_cols)
{
internal::conditional_aligned_delete_auto(m_data, m_rows*m_cols);
if (size)
m_data = internal::conditional_aligned_new_auto(size);
else
m_data = 0;
EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
}
m_rows = rows;
m_cols = cols;
}
The bolded lines are the ones that get hit before the line:
throw std::bad_alloc();
gets hit, which happens somewhere inside the internal::conditional_aligned_delete_auto(m_data, m_rows*m_cols); function call.
Why can’t I catch this exception from my client code? Is it because the Eigen library didn’t mark the resize function with throws? How can I make this code using the Eigen library recover from this malloc type error smoothly?
The problem was the exception being thrown by
resize()wasstd::exception. However, the code in the question explicitly tries to capture exceptions of typeint. This won’t work and thecatchblock will be passed.One way to fix this is by using a general “catch all” block:
The ellipses in the
catch(...)statement mean you don’t have to resolve exception type. All exceptions will be caught, regardless of type.However this appears to be not recommended. This may be analogous to the generic
catch( Exception e )statements in Java and C#, which is frowned upon in those languages for reasons stated here.