Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming Concepts: What should be done when an exception is thrown?

This does not really apply to any language specifically, but if it matters I am using VB.NET in Visual Studio 2008.

I can't seem to find anything really that useful using Google about this topic, but I was wondering what is common practice when an exception is thrown and caught but since it has been thrown the application cannot continue operating.

For example I have exceptions that are thrown by my FileLoader class when a file cannot be found or when a file is deemed corrupt. The exception is only thrown within the class and is not handled really. If the error is detected, then the exception is thrown and whatever function it was thrown in basically quits.

So in the code trying to create that object or call one of its members I use a Try...Catch statement. However, I was wondering, what should even do when this exception is caught? My application needs these files to be intact, and if they are not, the application is almost useless. So far I just pop up a message box telling the user their is an error and to reinstall. What else can I do, or better, what's common practice in these situations?

like image 361
Dooms101 Avatar asked Feb 28 '23 05:02

Dooms101


2 Answers

IMHO there are the following types of exceptions in a system:

  1. Recoverable exceptions - these are cases where the system might encounter an exception, but can then default to a state with which it can continue working & show a message to the user that it is can continue with the default option with the user selecting either "Continue", "Retry" or "Cancel" the operation.

  2. Non-recoverable exceptions - these are cases where the system has no way to continuing or has any default option. In this case, a user intervention is needed. So the system shows a message to the user with proper guidance of what needs to be done with options to either "Retry" or "Cancel" the operation

Depending on which type of exception you case falls into, I hope this might be useful.

like image 192
Sunny Avatar answered Apr 26 '23 10:04

Sunny


The common practice is to handle the exceptions you can, and pass on (with optional logging) the ones that you can't. If you hit a problem that just can't be fixed, then no amount of trying again will help and the correct response is to stop.

like image 37
Ignacio Vazquez-Abrams Avatar answered Apr 26 '23 09:04

Ignacio Vazquez-Abrams