Repository: opencv
Issue: imwrite_('1.jpg'): can't write data: unknown exception
Labels: question (invalid tracker)

### System Information

OpenCV python version: 4.8.0
Operating System / Platform: win 10
Visual Studio 2019 (v142) ISO C++17 标准 (/std:c++17)

### Detailed description

I have 120 frames of Mat type data cached in my memory, and after triggering the condition the data is read from the container and written as a local video file. After completing the operation this data is cleared. The next trigger performs the same operation, but occasionally the programme exits. I locate that there is something wrong with the opencv write Mat step, I can't catch this exception, after that I choose to use imwrite to write this problematic Mat data, and I get a printed error, so I can be sure that there is something wrong with the Mat data. Now I need to check whether the Mat data is valid or not. I have used some judgements and they are all fine, please help me to see if there is any other way, thanks.
 

### Steps to reproduce

	cv::VideoWriter writer;

						// 检查writer是否成功创建
if (!writer.open(t_upload.temp_path, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 2, cv::Size(1920, 1080), true)) {
							INFO("无法创建视频写入器");
							return;
						}

INFO("缓存文件路径:%s,t_frame size:%d", t_upload.temp_path.c_str(), t_frame.size());

						try {
							for (size_t index = 0; index < t_frame.size(); index++) {
								if (index >= t_frame.size()) break;  // 添加边界检查

								if (t_frame[index].empty() || !t_frame[index].data) {
									continue;
								}
								INFO("after:%d", index);
								if (index == 0)
								{
									// 判断mat是否有效（实际上它是无效的，因为它是空的）
									//* These judgements don't work.
									if (t_frame[index].empty() || t_frame[index].dims <= 0 || t_frame[index].channels() <= 0) {
										std::cout << "Mat is invalid." << t_frame[index].cols << ",h:" << t_frame[index].rows << std::endl;
									}
									else {
										//* isContinuous: true type：16
										std::cout << "Mat is valid,w:" << t_frame[index].cols << ",h:" << t_frame[index].rows << std::endl;
										std::cout << "Mat isContinuous:" << t_frame[index].isContinuous() << ";type:" << t_frame[index].type() << std::endl;
									}

									try
									{
										//* error，example：
										//	Mat is valid,w:1920,h:1080
										//	Mat isContinuous : 1; type:16
										//	imwrite_('1.jpg') : can't write data: unknown exception
										imwrite("1.jpg", t_frame[index]);
									}
									catch (...)
									{
										std::cout << "error frame\n";
										continue;
									}
								}
								writer.write(t_frame[index]);
								INFO("before:%d", index);
							}

							INFO("准备释放writer");
							writer.release();
							t_frame.clear();
							INFO("中间文件生成完毕");
						}
						catch (const cv::Exception& e) {
							handleException(&e, "OpenCV exception", writer, t_frame);
						}
						catch (const std::exception& e) {
							handleException(&e, "Standard exception", writer, t_frame);
						}
						catch (...) {
							handleException(nullptr, "Unknown exception", writer, t_frame);
						}

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [ ] There is reproducer code and related data files (videos, images, onnx, etc)