Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

error handling with imread()

Hello all, i am having a problem with the error handling in terms of the API "imread()". If i pass as command line a false file name, like "foobar" created randomly by "touch foobar" in bash. In my code i wrap the "imread()" into a try-catch block as follows:

cv:: Mat src;
         src =imread(argv[1]);
   catch( cv::Exception& e )
         //const char* err_msg = e.what();
         //std::cout << "exception caught: " << err_msg << std::endl;
         cout <<"wrong file format, please input the name of an IMAGE file" <<endl;
     return -1;

i commented the error message in catch block because i just want the program to exit. However, when i execute the program it still prints the following error message to stdout and the catch block is never executed:

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 140) > this->size() (which is 0) Aborted (core dumped)

I know this message comes from c++ standard library and i can't change them, is there still any way to get rid of the error message? the program is aborted now, what i am expecting is that it goes into the catch block and print something that can be specified by the programmer, like "wrong file format, please input the name of an IMAGE file"?

Besides, i'm using xubuntu 18.04 and opencv 3.2.0, thanks in advance!