Exception not caught

Hello, I am currently facing the problem outlined in these links:

However neither of them have been answered sufficiently or resolved my problem. Can anyone help me with this?

Those do not even mention OpenCV.

Doesn’t look like an OpenCV problem to me.

Please present a MRE that requires OpenCV to cause the issue. If this can be reproduced with any other library that throws exceptions (even a self-made dummy one), it’s not an OpenCV issue.

Otherwise, please take this to a C++ forum. This is the OpenCV forum.

I am asking a question about compiling openCV–this is related to open CV because I am able to use other OSS without issues. Are there any CMake settings that I need to use?

show us your specific issue. The links you provided show no issue with OpenCV. MRE required.

I am using the following method of building openCV:

UNBUFFER=$( (command -v unbuffer > /dev/null 2>&1 && echo "unbuffer") || true)

build_folder="/path/to/your/OCV4.8.0/opencv/builds/lax/debug"
rm -rf "${build_folder}"
mkdir -p "${build_folder}"

cd "${build_folder}" || return 1

mkdir -p install

/path/to/your/cmake/install/bin/cmake \
    -G "Unix Makefiles" \
    -D CMAKE_C_COMPILER=/path/to/your/gcc/bin/gcc \
    -D CMAKE_CXX_COMPILER=/path/to/your/gcc/bin/g++ \
    -D CMAKE_BUILD_TYPE="debug" \
    -D OPENCV_EXTRA_MODULES_PATH=/path/to/your/opencv_contrib/modules \
    -D CMAKE_INSTALL_PREFIX=install \
    -D CMAKE_SKIP_INSTALL_RPATH=YES \
    -D CMAKE_SKIP_RPATH=YES \
    -D WITH_AVFOUNDATION=OFF \
    -D WITH_1394=OFF \
    -D WITH_GPHOTO2=OFF \
    -D WITH_VTK=OFF \
    -D WITH_CAROTENE=OFF \
    -D WITH_CUDA=OFF \
    -D WITH_CUBLAS=OFF \
    -D WITH_CUFFT=OFF \
    -D WITH_EIGEN=OFF \
    -D WITH_LAPACK=OFF \
    -D WITH_NVCUVID=OFF \
    -D WITH_FFMPEG=OFF \
    -D WITH_GSTREAMER=OFF \
    -D WITH_VFW=OFF \
    -D WITH_GTK=OFF \
    -D WITH_ITT=OFF \
    -D WITH_IPP=OFF \
    -D WITH_OPENEXR=OFF \
    -D WITH_WIN32UI=OFF \
    -D WITH_PTHREADS_PF=OFF \
    -D WITH_PROTOBUF=OFF \
    -D WITH_V4L=OFF \
    -D WITH_DSHOW=OFF \
    -D WITH_OPENCL=OFF \
    -D WITH_OPENCL_SVM=OFF \
    -D WITH_OPENCLAMDFFT=OFF \
    -D WITH_OPENCLAMDBLAS=OFF \
    -D WITH_DIRECTX=OFF \
    -D WITH_MATLAB=OFF \
    -D WITH_WEBP=ON \
    -D BUILD_ANDROID_PROJECTS=OFF \
    -D BUILD_ANDROID_EXAMPLES=OFF \
    -D BUILD_opencv_cnn_3dobj=OFF \
    -D BUILD_opencv_cvv=OFF \
    -D BUILD_opencv_datasets=OFF \
    -D BUILD_opencv_dnn_objdetect=OFF \
    -D BUILD_opencv_dnn_superres=OFF \
    -D BUILD_opencv_dnns_easily_fooled=OFF \
    -D BUILD_opencv_dpm=OFF \
    -D BUILD_opencv_face=OFF \
    -D BUILD_opencv_freetype=OFF \
    -D BUILD_opencv_fuzzy=OFF \
    -D BUILD_opencv_hdf=OFF \
    -D BUILD_opencv_hfs=OFF \
    -D BUILD_opencv_img_hash=ON \
    -D BUILD_opencv_intensity_transform=OFF \
    -D BUILD_opencv_julia=OFF \
    -D BUILD_opencv_line_descriptor=OFF \
    -D BUILD_opencv_matlab=OFF \
    -D BUILD_opencv_mcc=OFF \
    -D BUILD_opencv_optflow=OFF \
    -D BUILD_opencv_ovis=OFF \
    -D BUILD_opencv_phase_unwrapping=OFF \
    -D BUILD_opencv_plot=OFF \
    -D BUILD_opencv_quality=OFF \
    -D BUILD_opencv_rapid=OFF \
    -D BUILD_opencv_reg=OFF \
    -D BUILD_opencv_rgbd=OFF \
    -D BUILD_opencv_saliency=OFF \
    -D BUILD_opencv_sfm=OFF \
    -D BUILD_opencv_shape=OFF \
    -D BUILD_opencv_stereo=OFF \
    -D BUILD_opencv_structured_light=OFF \
    -D BUILD_opencv_superres=OFF \
    -D BUILD_opencv_surface_matching=OFF \
    -D BUILD_opencv_text=OFF \
    -D BUILD_opencv_tracking=OFF \
    -D BUILD_opencv_videostab=OFF \
    -D BUILD_opencv_viz=OFF \
    -D BUILD_opencv_wechat_qrcode=OFF \
    -D BUILD_opencv_xfeatures2d=OFF \
    -D BUILD_opencv_ximgproc=OFF \
    -D BUILD_opencv_xobjdetect=OFF \
    -D BUILD_opencv_xphoto=OFF \
    '/path/to/your/OCV4.8.0/opencv'

${UNBUFFER} make -j "$(nproc)" 2>&1 | tee make.out
${UNBUFFER} make -j "$(nproc)" install 2>&1 | tee make-install.out

The main code that runs openCV contains this:

try{
    cv::imencode(bad_extension, correct_matrix, correct_v);
    return 0;
}
catch (...)
{
  return -1;
}

However the exception is NOT being caught.

OpenCV(4.8.0) Error: Unspecified error (could not find encoder for the specified extension) in imencode, file /path/to/opencv/modules/imgcodecs/src/loadsave.cpp, line 985
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.8.0) 

/path/to/opencv/modules/imgcodecs/src/loadsave.cpp:985: error: (-2:Unspecified error) could not find encoder for the specified extension in function 'imencode'

/usr/local/bin/runcmdfile[43]: .: line 76: 21316: Abort(coredump)

The main code is compiled with gcc 9.2 and linked statically with openCV.

/path/to/gcc-9.2.0.R6/bin/g++ \
    -std=c++14 -I- -g \
    /path/to/src/processImages.cpp -o \
    /path/to/tmp/dev_mva-vbviya_laxddc_en_processImages_709296546 \
    --coverage \
    -fprofile-dir=%q{UNXCOVDATADIR} -fprofile-abs-path \
    -c -fPIC \
    -D_XOPEN_SOURCE=700 -D_BSD_SOURCE \
    -D_SVID_SOURCE -pthread -shared -fno-strict-aliasing \
    -funsigned-char -W -Wall -Wno-unused-function \
    -Wno-missing-field-initializers -Wno-unused-parameter \
    -Wno-char-subscripts -Wno-sign-compare -Wno-unused-value \
    -Wno-switch -Wno-missing-braces -fsigned-char -fmessage-length=0

looks like you call imencode() with invalid arguments.

that’s still not a MRE.

perhaps we should just stop here.

Yes the I am calling it with invalid arguments and it should be caught with the try-catch. It seems like you don’t have the expertise to solve this issue. I will try elsewhere.

This is what you linked to in your first post, commenting “almost identical to”. It does not require OpenCV.

It does seem to involve an old version of GCC (why?) and the program being run under gcov, i.e. an instrumented build or debugging context. You didn’t suspect either of those so far.

I hope you’ll get your answer. I wouldn’t bet on it though.