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;
try
{
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!
from the documentation "If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL )."
can you try to update to a more recent opencv version, which has better error handling there ?
3.2.0 vs. master
I will try it as the last option, but IMO, 3.2.0 is recent enough for "imread()" as the program doesn't terminate on ubuntu 16.04 with the same version, i.e., 3.2.0 of OpenCV, even passed with a file with the false format. What are you trying to say by linking the above links? I see no difference between them in the source code from "maxlen = fread( (void*)signature.c_str(), 1, maxlen, f );" to "static ImageEncoder findEncoder( const String& _ext )"
well it may be, just don't give it empty files ;)
opencv uses
std::string
, 3.2 uses a "selfmade"cv::String
, and imho, that one does not likesubstr(0,0)
on an empty String. (don't have 3.2 around, so i can't prove it, but maybe you can make a small test ?)I myself know of course how to run the program properly and won't give it an empty file, the thing is that I can't assume that my client user also knows how, so it's my task to consider all possibilities what may be passed by the client user. If the user passes an empty file I want to notify them that an image file should be passed, rather than the program just terminates. This can already be accomplished on ubuntu 16.04 with OpenCV 3.2.0 installed by checking "if(src.empty())", but on Xubuntu 18.04 with the same version of OpenCV the program just terminates at "imread()". What should i test exactly?