Question

I am currently writing a wrapper class based on ObjectWrap for the armadillo linear algebra library. I have noticed that when an exception is emitted from armadillo, it is not caught in the add-on code even when i try to handle the exception:

if(args[0].isString()){
    try{
        std::string s(*v8::String::AsciiValue(args[0]->ToString()));
        MatrixWrap* matrix = new MatrixWrap(s);
        matrix->Wrap(args.This());
    }catch(exception& e){
        cout << "caught exception" << endl;  // this code is never called.
    }
}

The corresponding corresponding constructor is given below (_matrix is an instance of arma::mat and throws an exception if the string is not correctly formatted):

MatrixWrap::MatrixWrap(string s):_matrix(s){

}

When I run the code, I get the following output:

> var arma = require('./build/Release/armadillo');
> var matrix = new arma.Matrix('1 0; 0 1');
> matrix.print();
1.0000        0
    0   1.0000

> var B = new arma.Matrix('1 0; 0');

error: Mat::init(): inconsistent number of columns in given string
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6

It's as if the exception is not handled despite the catch clause

Was it helpful?

Solution

Instead of catch(exception& e), try using catch(...) which should catch all exceptions.

Depending on the error, Armadillo throws std::logic_error, std::bad_alloc, or std::runtime_error

OTHER TIPS

Modified build script to enable exception handling:

{
    "targets": [
        {
            'target_name': 'armadillo',
            'sources':[ "src/addon.cpp", "src/MatrixWrap.cpp"],
            'link_settings': {
                'libraries': ['-larmadillo']
            },
            'cflags': ['-fexceptions'],
            'cflags_cc': ['-fexceptions']
        }
   ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top