# Catching Opencv assertion errors: (-215:Assertion failed) 0 \<= roi.x && 0 \<= roi.width && roi.x + roi.width \<= m.cols && 0 \<= roi.y && 0 \<= roi.height && roi.y + roi.height \<= m.rows in function ‘Mat’

**URL:** <https://forum.opencv.org/t/catching-opencv-assertion-errors-215-assertion-failed-0-roi-x-0-roi-width-roi-x-roi-width-m-cols-0-roi-y-0-roi-height-roi-y-roi-height-m-rows-in-function-mat/18151>\
**Category:** C++\
**Tags:** programming\
**Created:** [July 2, 2024, 10:54pm UTC](https://forum.opencv.org/t/catching-opencv-assertion-errors-215-assertion-failed-0-roi-x-0-roi-width-roi-x-roi-width-m-cols-0-roi-y-0-roi-height-roi-y-roi-height-m-rows-in-function-mat/18151 "2024-07-02T22:54:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rs-ixz](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@rs-ixz](https://forum.opencv.org/u/rs-ixz)\
**Post date:** [July 2, 2024, 10:54pm UTC](https://forum.opencv.org/t/catching-opencv-assertion-errors-215-assertion-failed-0-roi-x-0-roi-width-roi-x-roi-width-m-cols-0-roi-y-0-roi-height-roi-y-roi-height-m-rows-in-function-mat/18151/1 "2024-07-02T22:54:37Z")

</div>

Sometimes our c++ app may supply input dimensions that are incompatible for a Mat ROI, which results in the following exception from the Opencv library.

Although we are able to debug this in a full debugging environment, is there an easy way to detect the faulting line in application code that causes this assertion failure?

Surrounding every opencv api call with Try/catch is something we would like to avoid and have try/catch only at the higher levels; but this does not give us enough information currently for these type of assertion failures.

> **Exception caught: OpenCV(4.5.3-pre) /home/t1/gani/src/opencv/modules/core/src/matrix.cpp:811: error: (-215:Assertion failed) 0 \<= roi.x && 0 \<= roi.width && roi.x + roi.width \<= m.cols && 0 \<= roi.y && 0 \<= roi.height && roi.y + roi.height \<= m.rows in function ‘Mat’**

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 3, 2024, 5:26am UTC](https://forum.opencv.org/t/catching-opencv-assertion-errors-215-assertion-failed-0-roi-x-0-roi-width-roi-x-roi-width-m-cols-0-roi-y-0-roi-height-roi-y-roi-height-m-rows-in-function-mat/18151/2 "2024-07-03T05:26:23Z")

</div>

however you calculate those regions, your math is broken.

this is a programming/logic problem.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 3, 2024, 8:44am UTC](https://forum.opencv.org/t/catching-opencv-assertion-errors-215-assertion-failed-0-roi-x-0-roi-width-roi-x-roi-width-m-cols-0-roi-y-0-roi-height-roi-y-roi-height-m-rows-in-function-mat/18151/3 "2024-07-03T08:44:38Z")

</div>

an exception is a message to you, that your code is doing something wrong  
(and that the code / assumption behind it must be fixed.)

do your own (similar) checks _before_ calling the resp. function, where still you can react accordingly.
