What is the best way to handle file and image upload exceptions?

Except that there is no solution or alternative, for example, if you do not open the image, I usually do something like:

try { img.load_from_file("my_image.png"); } catch( const image_loading_exception& e ) { /* Could Not Load Image: 'my_image.png' ! */ string err = "Could Not Load Image: " + e.what() + " !"; // pro::message_box::show( err ); } 

Now, is this really good error handling? Should I do something more descriptive or try to solve the problem. Displaying an error message window is the only thing I can think of when handling an error loading a file or file. What are you guys doing?

+4
source share
2 answers

It depends on the requirements:

  • If the program cannot continue, because there is no critical image, it should stop execution and inform the user what happened.
  • if the user entered an incorrect parameter (for example, an invalid file name), they should be informed and ask to enter it again
  • If this is something that is not important at all, then simply register the message without informing the user
+4
source

What you have to do is highly contextual.

If such code was placed in the handler for the "File | Open" option, where you open the image file, then this seems acceptable.

On the other hand, if it was placed deep inside some skins for your application, then this is most likely bad. Firstly, you do not know when this will be done, and if the message field will be appropriate. You do not know how often this will be performed (if it is one of 100 images and then shows 100 messages, this is not good ...). In the fatal case, this can lead to a dead loop, if in any way to show the message window (or reject it by the user) will lead to the repeated execution of this code.

Therefore, if you do not provide context, it is difficult to provide a reasonable and useful answer.


Also note that if you don’t know the details of load_from_file or its guarantee for image_loading_exception content that displays what() user, this is not a good idea. This line is likely to be technical information (or maybe) is not useful and incomprehensible to the user. (On the other hand, if this is just a tool for you or other programmers, then this may be acceptable - context again!)

In addition, if you do not know that image_loading_exception will do this in your what() , you can consider capturing typical cases (missing file, access denied, empty file, damaged file, etc.) and show the highlighted one ("oriented to user ") message, possibly leaving what() as default hidden details or writing them down somewhere.

+2
source

Source: https://habr.com/ru/post/1402387/


All Articles