Programming concepts: what needs to be done when an exception is thrown?

This is not specific to a specific language, but if it is important, I use VB.NET in Visual Studio 2008.

I canโ€™t find anything really useful using Google on this topic, but I was wondering what is the usual practice when an exception is thrown and caught, but since it was thrown, the application cannot continue to work.

For example, I have exceptions that cause my FileLoader class when a file cannot be found or when a file is considered damaged. The exception extends only inside the class and is not actually handled. If an error is detected, an exception is thrown and any function that he has chosen mainly terminates.

So, in the code trying to create this object or call one of its members, I use the Try ... Catch operator. However, I was wondering what to do when this exception is caught? My application requires these files to be intact, and if not, the application is almost useless. So far, I just opened a window with a message to the user about the error and reinstallation. What else can I do, or better, what is the common practice in these situations?

+4
source share
6 answers

IMHO, the following types of exceptions exist in the system:

  • Excluded exceptions are cases where the system may encounter an exception, but then can by default indicate the state with which it can continue to work and show the user a message that it can continue the default setting when the user selects either Continue , "Repeat", or "Cancel" operation.

  • Exceptions that cannot be recovered are cases where the system is unable to continue or has some default option. In this case, user intervention is required. Thus, the system displays a message to the user with appropriate guidance on what needs to be done using the "Retry" or "Cancel" operation

Depending on the type of exception to which the case applies, I hope this can be useful.

+3
source

A common practice is to handle exceptions that you can and pass (with additional logging) those that you cannot. If you hit a problem that simply cannot be fixed, then no attempt to retry will help, and the correct answer will be stopped.

+2
source

In your case, I think that exactly what you are doing - displaying the error to the user using a user-friendly message, and then exiting the operation - is exactly what you should do.

I tend to group an exception into two categories: Exceptions from which you can recover and exceptions from which you cannot recover. Obviously, if you can recover from an exception without a user, even knowing that there was an error, then this is the best option. But in some cases, the application simply cannot move forward, and you need to somehow abandon the current operation.

Yours does not look like a web application, but if so, it is often useful to send an email to the developer when an unexpected error occurs, so that the developer can make the appropriate changes.

Also note that many frameworks have the ability to catch all uncaught exceptions without having to put all this into the Try Catch block. For example, ASP.NET 3.5 uses the Global.asax class, which catches all exceptions. Since our application is based on a web interface, this allows any errors that we did not expect to receive through this class, and we can send error messages to developers and display the corresponding user error page. Although, of course, if you are expecting a possible exception in a particular block of code, then you can just put Try-Catch around this block, but for everyone else it is useful.

+2
source

There are no real general answers to this particular โ€œproblemโ€, but here are some comments that may help you.

  • Do not break your abstractions. If your code is called at a level that deals with objects, but uses the SQL data store below, it should not skip the SQL exception.

  • Do not lose information. Sometimes people catch exceptions and simply ignore them or print a message like "something went wrong." Without sufficient detail (either in the logs or on the screen), you cannot determine where / when the problem occurred, and you can spend quite a lot of time debugging.

  • Exception hierarchies - this may not apply to all languages, but if you can structure your exceptions hierarchically and can catch trees from them, and not just one, a lot of code becomes pretty clean.

    / li>

Your approach sounds decent in your case. Notify the user of a fatal error and termination. In order to borrow an excellent answer from Sunny, it is not restored, so you must request user intervention. Personally, I would prefer a warning and reset to default or something internal, if possible, rather than a crash and a reinstall request.

+1
source

In your particular situation, that is the only way to go.

In another situation ... well, it really depends on the situation.

0
source

I often come across this situation with the internal tools that I develop. I usually process it in the same way as you do; show the user an error message and ask him to try again or exit.

0
source

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


All Articles