'Is it ok that valgrind shows errors that I catch using try-catch?

I have class Matrix and some tests for constructors:

try {
    std::cout << "Matrix with negative size:" << std::endl;
    Matrix<int> err_m(-1, 3);
} catch (ExceptionMatrix &err){
    std::cerr << err.what() << "\n\n";
}

Valgrind shows this:

 Argument 'size' of function __builtin_vec_new has a fishy (possibly negative) value: -1
==741==    at 0x483C583: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==741==    by 0x116AC3: Matrix<int>::_allocateMemory(unsigned long, unsigned long) (matrix_private.hpp:56)
==741==    by 0x111715: Matrix<int>::Matrix(unsigned long, unsigned long) (matrix_constructors_and_eqoperators.hpp:3)
==741==    by 0x10BDB1: main (main.cpp:10)
==741==
**741** new/new[] failed and should throw an exception, but Valgrind
**741**    cannot throw exceptions and so is aborting instead.  Sorry.
==741==    at 0x483B3CC: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==741==    by 0x483C5F5: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==741==    by 0x116AC3: Matrix<int>::_allocateMemory(unsigned long, unsigned long) (matrix_private.hpp:56)
==741==    by 0x111715: Matrix<int>::Matrix(unsigned long, unsigned long) (matrix_constructors_and_eqoperators.hpp:3)
==741==    by 0x10BDB1: main (main.cpp:10)

In function _allocateMemory I also use try-catch. Should valgrind show this error?

template <typename T>
SharedPtr<typename Matrix<T>::MatrixRow[]> Matrix<T>::_allocateMemory(size_t rows, size_t cols) {
    SharedPtr< MatrixRow[] > data = nullptr;
    try {
        data.reset(new MatrixRow[rows]);
        for (size_t i = 0; i < rows; i++)
            data[i].reset(new T[cols], cols);
    }
    catch (std::bad_alloc &err) {
        time_t cur_time = time(NULL);
        auto curtime = localtime(&cur_time);
        throw MemoryError(asctime(curtime), __FILE__, __LINE__, "_allocateMemory function error");
    }

    return data;
}


Solution 1:[1]

As already mentioned in the comments

**741** new/new[] failed and should throw an exception, but Valgrind
**741**    cannot throw exceptions and so is aborting instead.  Sorry.

Valgrind does not support throwing exceptions from operator new.

Adding this functionality would be (in my opinion) a huge job. It would entail re-implementing the exception stack walking code in libgcc_eh (or the equivalent for llvm, I assume that they are compatible). There is already a fair bit of stack walking code (for generating error messages) which may be reusable to some extent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Floyd