I mean, I knew all the language rules about throwing, try {} catch {}, but I'm not sure if I use them correctly in the real world. See the following example:
We have a big piece of scientific code that did all kinds of image processing, recently we decided to pick it up and make it more reliable. One commonly used routine is void rotate_in_place (float * image, image_size sz) ;
To make it more reliable, add some health check at the beginning of the code:
void rotate_in_place(float* image, image_size sz) { // rotate_in_place does not support non-square image; if (sz.nx != sz.ny) throw NonSquareImageError; // rotate_in_place does not support image too small or too large if (sz.nx <= 2 || sz.nx > 1024) throw WrongImageSizeError; // Real rode here ..... }
Now the problem is that rotate_in_place () is used in over 1000 places, I have to wrap every call to rotate_in_place () with try {} catch {}, it looks like the code will be incredibly bloated. Another possibility is not to wrap up any attempt to {} catch {} and exit the program, but how is this different from using
if (sz.nx != sz.ny) { cerr << "Error: non-squared image error!\n"; exit(0); }
In short, I am not so sure of the real benefits of using throws, attempts, catches, good offers?
source share