Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wouldn’t handling an exception be a better idea

1)

1 - Only handle exceptions that you can actually do something about, and
2 - You can't do anything about the vast majority of exceptions

a) I assume that “By not handling an exception” the text is suggesting that we should let the exception bubble up the stack, where runtime will abort our application?!

b) But why is letting the runtime abort the exception preferred over catching an exception, logging it and then informing the user of failure? Only difference between the two is that in the latter case application isn’t aborted

For example, if database goes down, why should the whole program crash ( due to not handling an exception ), if we can instead catch the exception, log it and notify user of failure and that way we can keep the program up and running

2) If you know that exception potentially raised by some block of code can’t be handled, should you include this code inside a try-finally block or is it better to leave it outside any try-finally blocks?

Thank you

like image 714
user437291 Avatar asked Sep 19 '10 18:09

user437291


4 Answers

No, the guideline is not to catch an exception you cannot do anything about except at the top-level of your application or thread.

You should try to avoid letting your application crash - log the information somewhere, give a message to your user explaining what happened, and tell them how to report the error to you. Perhaps also try to save their unsaved data in a recovery file so that the next time the application starts it can offer the option to attempt to recover their lost work.

like image 77
Mark Byers Avatar answered Sep 29 '22 09:09

Mark Byers


Try looking at it this way... The database goes down. How do you know? Because you get an timeout/an exception/something. But your application probably isnt getting the exception. ADO.NET/Linq to SQL/Entity Framework/Whatever data provider you are using is actually getting the exception and throwing it to your application. To me, this is what that advice is advising: as a component designer, prefer to throw exceptions you can't do anything about.

For the database down example, is there anything the ADO.NET data provider can do? Can it bring a server back up? Repair network connections? Reset permissions? No. So it doesn't handle the exception, it throws it.

The guideline you cite is for component development, not the outer edge of a run-time boundary (a thread or application). At that level, it would be correct to make a decision on how to handle exception that have bubbled that far.

like image 26
Jonathan Bates Avatar answered Sep 29 '22 09:09

Jonathan Bates


I think the person you are quoting suggests that you should let the exception bubble up the stack until something higher up can make sense of it or until it reaches the top of the call stack where you do have code that would log it, or display a error message to the user then exit your program if it is fatal, or carry on if it is not.

Sometimes it may be better to not continue executing the program - if you get a OutOfMemoryException for example or some other situation where the programs actions are undefined - a potential disaster.

like image 38
Callum Rogers Avatar answered Sep 29 '22 11:09

Callum Rogers


I think the key to

Only handle exceptions that you can actually do something about

is that you should only handle the exception if you can carry on from that point in your application.

To take a trivial example.

If you're looking for a file on the user's system and it's not there when it should be you should raise the "file not found" exception. Now if you can recover from this (say by simply recreating the file) then do so and carry on. However, if the file can't be recreated then you shouldn't let your program carry on.

However, this doesn't mean you can't have a top level catch all exception handler in your main program to display a friendly message to the user, perhaps to log the exception or even mail it to you the developer.

like image 25
ChrisF Avatar answered Sep 29 '22 10:09

ChrisF